Write a Java program to accept N array Elements and calculate Sum and Average of array elements.

Write a Java program to accept N array Elements and calculate Sum and Average of  array elements.


program:

import java.util.Scanner;
public class Array_Sum

public static void main(String[] args)
{
int n, sum = 0; 
float avg;
Scanner sc= new Scanner(System.in); System.out.print("Enter no. of elements you want in array:");
n = sc.nextInt();

int a[] = new int[n];
//int a[ ]={10,50,20,40,30};
System.out.println("Enter all the elements:"); 
for(int i = 0; i < n; i++)
{
a[i] = sc.nextInt();
sum = sum + a[i];
}

System.out.println("Sum is:"+sum);
avg=(float)sum/n;
System.out.println("Average is:"+avg);
}
}


Output:


Enter no. of elements you want in array :

Enter all the elements: 
1
2
3
4
5
Sum is:15
Average is:3.0


Comments

Popular posts from this blog

Control Statements:Selection statement ,Iteration statement and Jump statement in Java

Applets - Inheritance hierarchy for applets, differences between applets and applications, life cycle of an applet, passing parameters to applets, applet security issues.

Java Database Connectivity (JDBC) ,Steps to connect to the database in Java