Programming Examples
Java program to accept n elements in array and find its sum and Average
Write a java program to accept n elements in array and find its sum and Average.
import java.util.*;
class SumAvg
{
public static void main(String arr[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of Array : ");
int n=sc.nextInt();
int num[]=new int[n];
int sum=0;
float avg;
System.out.println("Enter the "+n+" Elements of Array");
for(int i=0;i<n;i++)
{
num[i]=sc.nextInt();
}
System.out.println("Array Elements Are : ");
for(int i=0;i<n;i++)
{
System.out.print(num[i]+" ");
sum=sum=num[i];
}
avg=(float)sum/n;
System.out.println("\nSum of All Elements :"+sum);
System.out.println("Avg of All Elements :"+avg);
}
}
Output
Enter the size of Array :
5
Enter the 5 Elements of Array
3
4
5
3
4
Array Elements Are :
3 4 5 3 4
Sum of All Elements :4
Avg of All Elements :0.8