Q. VII, Write a program to input 10 integers and find the sum and average of the
same.
Answers
Question:-
➡ Write a program to input 10 numbers and find the sum and average of the same.
Program:-
Here is your program written in Java.
1. Using Scanner Class.
import java.util.*;
class SumAvg
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in); //creating object of Scanner class.
System.out.println("Enter 10 numbers.");
double s=0.0,a=0;
for(int I=1;I<=10;I++)
{
System.out.print("Enter: ");
s+=sc.nextDouble();
}
a=s/10.0;
System.out.println("Sum of all the 10 numbers is: "+s);
System.out.println("Average of all the 10 numbers is: "+a);
}// end of main() method.
} // end of class.
First of all, we will create a for loop within which we will input 10 numbers and then add them and store them in variable s.
After the loop terminates, we will divide the sum by 10.0 and we will get the average of them.
Now, we will print the sum and average.