This Java code defines a class sum_avg that contains a main method. The main method prompts the user to enter a limit and reads the input using a Scanner object. The code then reads n numbers from the user using a for loop.
Inside the loop, the code prompts the user to enter a number and reads the input using a Scanner object. The code then adds the entered number to a variable sum which stores the sum of all the entered numbers.
After the loop ends, the code prints the sum of all the entered numbers using the System.out.println method. It then calculates the average of the entered numbers by dividing the sum by the limit n and prints it using the System.out.println method.
Example:
10,20,30,40,50.
Sum of given numbers = 150.
Average of given numbers = 30.
This is because the sum of the entered numbers is 10+20+30+40+50=150 and the average of the entered numbers is 150/5=30.
import java.util.Scanner; //Write a program to find the sum and average of given n numbers. public class sum_avg { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("Enter The Limit: "); int n=in.nextInt(); int sum=0,a; for(int i=1;i<=n;i++) { System.out.println("Enter The Number "+i+": "); a=in.nextInt(); sum+=a;//sum=sum+a; } System.out.println("The sum of given numbers is : "+sum); System.out.println("The Average of given numbers is : "+sum/n); } }
The sum of given numbers is : 150 The Average of given numbers is : 30To download raw file Click Here
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions