Problem Statement - Array Filling – Name and Marks – Average and Deviation display
Write a program to accept name and total marks of N number of students in two single 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]
Solution
TC++ #7198
Run Output
Enter number of students: 3
Student 1
Enter name: Nani
Enter marks: 95
Student 2
Enter name: Kittu
Enter marks: 99
Student 3
Enter name: Bittu
Enter marks: 94
Average is 96.0
Deviation of Nani is -1.0
Deviation of Kittu is 3.0
Deviation of Bittu is -2.0
public class Student {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print(“Enter number of students: “);
int n = scanner.nextInt();
String[] name = new String[n];
int[] totalmarks = new int[n];
for (int i = 0; i < n; i++) {
System.out.println(“Student ” + (i + 1));
System.out.print(“Enter name: “);
name[i] = scanner.next();
System.out.print(“Enter marks: “);
totalmarks[i] = scanner.nextInt();
}
int sum = 0;
for (int i = 0; i < n; i++) {
sum = sum + totalmarks[i];
}
double average = (double) sum / n;
System.out.println(“Average is ” + average);
for (int i = 0; i < n; i++) {
double deviation = totalmarks[i] – average;
System.out.println(“Deviation of ” + name[i] + ” is ” + deviation);
}
}
}