C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Deviation Program - Java Programs

Write a program to accept name and total marks of N number of students in two subscript array name[] and totalMarks[].

Calculate and print:

(i) The average of the total marks obtained by N number of students.

[average = (sum of total marks of all the students) / N]

(ii) Deviation of each student’s total marks with the average.

[deviation = total marks of a student – average]

import java.util.Scanner; class Deviation{ public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Number of students: "); int n = Integer.parseInt(in.nextLine()); String name[] = new String[n]; int totalMarks[] = new int[n]; double total = 0.0; for(int i = 0; i < n; i++){ System.out.print("Name: "); name[i] = in.nextLine(); System.out.print("Total marks: "); totalMarks[i] = Integer.parseInt(in.nextLine()); total += totalMarks[i]; } double avg = total / n; System.out.println("Average marks: " + avg); for(int i = 0; i < n; i++){ double d = totalMarks[i] - avg; System.out.println("Deviation of " + name[i] + " = " + d); } } }

Output

 
 OUTPUT  : 
Number of students: 5
Name: Ajay
Total marks: 78
Name: Anand
Total marks: 95
Name: Navita
Total marks: 70
Name: Bala
Total marks: 59
Name: Santosh
Total marks: 69

Average marks: 74.2
Deviation of Ajay = 3.799999999999997
Deviation of Anand = 20.799999999999997
Deviation of Navita = -4.200000000000003
Deviation of Bala = -15.200000000000003
Deviation of Santosh = -5.200000000000003