Write a program to calculate the average height of all the students of a class. The number of students and their heights in a class are entered by user.
I have written the code below with comments. for loop is used to take input of height for different students one by one.
import java.util.*;
class Height {
public static void main (String[] args) {
//declaring variables
int num;
float total=0, avg;
Scanner s = new Scanner(System.in);
//taking no. of students as input
num = s.nextInt();
//enter height of each student
for(int i=1; i<=num; i++) {
float ht = s.nextFloat();
total = total + ht;
}
//calculate average height (total height)/(no. of students)
avg = total/num;
System.out.println("Average height: " + avg);
}
}
Here's a concise Python program for calculating the average height of students based on user input:
average_height = sum(map(int, input("Enter heights separated by space: ").split())) / int(input("Enter the number of students: "))
print(f"Average height of the class: {average_height}")
Auto Detailing Services in West Palm Beach FL