passing 1D array
Write a program to show passing of 1D array.
import java.util.*;
class P16ArrayPassing
{
public static void main()
{
System.out.println("Enter the no. of integer you want to input ");
Scanner in = new Scanner(System.in);
int n=in.nextInt();
int[] a= new int[n];// create an array of length n
for(int i=0;i<n;i++)
{
System.out.print(" \n Enter integer "+(i+1) + "\t");
a[i]=in.nextInt(); // store in value input by user in array
}
int[] result=passingArray(a,n);
System.out.println("\nSUM of Numbers "+ result[0]);
System.out.println("\nMULTIPLICATIONS of Numbers "+ result[1]);
System.out.println("\naverage of Numbers "+ result[2]);
}
static int[] passingArray(int[] ar, int n)
{
int sum=0, mul=1;
for(int i=0;i<n;i++)
sum= sum + ar[i]; // sum is calculated
for(int i=0;i<n;i++)
mul= mul * ar[i];// multiplication is calculated
int avg = sum/n; // average is calculated
int[] r = new int[]{sum,mul,avg};
return r;
}
}
Comments
Post a Comment