# Field Manual for Hackers

## Java Lab | Find the sum and mean of N number using array

1. import java.util.Scanner;
class Mean
{
public static void main(String Args[])
{
System.out.println("How many numbers you want to enter?");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int sum=0;
float avg;
int AR[]=new int[n];
System.out.println("enter the values");
for(int i=0;i<AR.length;i++)
{
AR[i]=sc.nextInt();
sum=sum+AR[i];
}
System.out.println("SUM IS "+sum);
avg=(float)sum/n;
System.out.println("MEAN IS "+avg);
}
}

2. 1.
import java.util.Scanner;
public class JavaApplication7 {

public static void main(String[] args) {

Scanner obj=new Scanner(System.in);
int n;
System.out.println("Enter the value of n");
n=obj.nextInt();
int arr[]=new int[n];
int sum=0;
double avg;
int i;
System.out.println("Enter the array");
for(i=0;i<n;i++)
{
arr[i]=obj.nextInt();
sum=sum+arr[i];
}
System.out.println("The sum is="+sum);
avg=sum/n;
System.out.println("The average is="+avg);
}
}

3. import java.util.Scanner;

class Test{
public static void main(String a[]){
int ar[]=new int[100];
int i,number;
double sum=0;
System.out.print("Enter the number of elements: ");
Scanner o=new Scanner(System.in);
number=o.nextInt();
System.out.println("Now enter the values of array: ");
for(i=0;i<number;i++){
ar[i]=o.nextInt();
sum+=ar[i];
}
System.out.println("The sum of values entered is: "+sum);
System.out.println("The mean of values entered is: "+(sum/number));
}
}

OUTPUT
Enter the number of elements: 6
Now enter the values of array:
2 5 4 6 8 4
The sum of values entered is: 29.0
The mean of values entered is: 4.833333333333333

4. This comment has been removed by the author.

5. import java.util.Scanner;
class ArrayMean
{
public static void main(String args[])
{double Total=0;
Scanner scan=new Scanner(System.in);
System.out.println("How many number you want to add");
int n=scan.nextInt();
double ar[]=new double[n];
System.out.println("Enter Numbers");
for (int i=0;i<n;i++)
{
ar[i]=scan.nextDouble();
Total+=ar[i];
}
double Average=Total/n;
System.out.println("Your Sum is "+Total);
System.out.println("Your Mean is "+Average);
}
}

6. import java.util.Scanner;
public class sumofarray
{
public static void main(String ar[])
{
Scanner scan = new Scanner(System.in);
int sum = 0 , n;
int []a = new int[50];
System.out.println(" Enter the size of array (max size is 50)");
n = scan.nextInt();
System.out.println(" Enter the elements of array");
for( int i = 0;i < n ; i++)
{
a[i] = scan.nextInt();
}
System.out.print("the sum of all the elements is :");
for( int i = 0;i < n ; i++)
{
sum += a[i];
}
System.out.print(+sum);
}
}

output:
Enter the size of array (max size is 50)
5
Enter the elements of array
45 28 97 23 13
the sum of all the elements is :206

7. import java.util.Scanner;
class Array{
public static void main(String ar[]){
Scanner obj=new Scanner(System.in);
int n,i,sum=0;
float mean;

System.out.println("how many elements are there");
n=obj.nextInt();
int arr[]=new int[n];
for(i=0;i<n;i++)
{
System.out.println("enter the element"+(i+1));
arr[i]=obj.nextInt();
sum=sum+arr[i];
}
mean=sum/n;
System.out.println("the sum is"+sum);
System.out.println("the mean is"+mean);
}

}

8. import java.util.Scanner;
class Mean{
public static void main(String args[]){
int size=0,i=0,n;
double mean=0.0,sum=0;
Scanner scan=new Scanner(System.in);
System.out.println("enter the size of array");
size=scan.nextInt();
int ar[]=new int[size];

for(i=0;i<size;i++)
{
System.out.println("enter the value"+(i+1));
ar[i]=scan.nextInt();
sum=sum+ar[i];
}
System.out.println("sum is:"+sum);
{
mean=sum/size;
System.out.println("mean is:"+mean);
}
}
}

9. import java.util.Scanner;
class Sumarray{
public static void main(String a[]){
Scanner scan=new Scanner(System.in);
int n,i,sum=0;
double mean;

System.out.println("Enter the no. of values:");
n=scan.nextInt();
int ar[]=new int[n];
System.out.println("Enter the values:");
for(i=0;i<n;i++){
ar[i]=scan.nextInt();
sum=sum+ar[i];
}
mean=sum/n;
System.out.println("The sum is"+sum);
System.out.println("The mean is"+mean);
}
}

10. import java.util.*;
class Suma{
public static void main(String ar[]){
Scanner s=new Scanner(System.in);
int n,i,sum=0;
double mean;
System.out.print("Enter the no. of values:");
n=s.nextInt();
int a[]=new int[n];
System.out.println("Enter the values:");
for(i=0;i<n;i++)
{
a[i]=s.nextInt();
sum=sum+a[i];
}
System.out.println("The sum is:"+sum);
mean=sum/n;
System.out.println("The mean is:"+mean);
}
}