Sunday, March 31, 2019

SUM AND AVERAGE OF N ELEMENTS IN DYNAMIC ARRAY IN CORE JAVA


Sum and average of n numbers using dynamically array                           EDITED BY SAYANI


Code:
import java.util.Scanner;
class arr
{
int a[],length;
void iplength(int x)
{
length=x;
}
void iparray()
{
a=new int [length];
Scanner j=new Scanner(System.in);
for(int i=0;i<length;i++)
{
a[i]=j.nextInt();
}
}
void op()
{
 int sum=0;
for(int i=0;i<length;i++)
{
sum=sum+a[i];
}
System.out.println("Sum="+sum);
double avg=(double)sum/(double)length;
System.out.println("Average="+ avg);
}
}
class arr1
                {
                                public static void main(String args[])
                                {
                                                arrob=new arr();
                                                ob.iplength(5);
                                                ob.iparray();
                                                ob.op();
                                }
                }

Output:E:\>javac arr1.java

E:\>java arr1
2
4
5
7
9
Sum=27
Average=5.4

E:\>


No comments:

Post a Comment