I\'m trying to write a program that incorporates the following criteria, but I\'
ID: 3560454 • Letter: I
Question
I'm trying to write a program that incorporates the following criteria, but I'm REALLY new at programming and don't understand how to get started yet. If I could get some help with step-by-step instructions or at least some guide on how to get started, I'd appreciate it!
As an example, if the user enters 1, 2, 3, 4, and 5 then one possible output could be:
You entered 1, 2, 3, 4, and 5.
The sum of those numbers is 15.
The product of those numbers is 120.
The largest number entered is 5.
The smallest number entered is 1.
The average of the numbers entered is 3.
Explanation / Answer
import java.util.*;//to use scanner class
public class ArrayDemo
{
public static void main(String args[])
{
int a[]=new int[5];
Scanner sc=new Scanner(System.in);//use to take input
int i;
for(i=0;i<a.length;i++)//arrayname.length gives the length of array
{
System.out.println("Enter number:");
a[i]=sc.nextInt();
}
int sum=0;
long pro=1;
int max=a[0];
int min=a[0];
for(i=0;i<a.length;i++)
{
sum=sum+a[i];
pro=pro*a[i];
if(a[i]>max)
max=a[i];
if(a[i]<min)
min=a[i];
}
float avg=sum/a.length;
System.out.print("You entered");
for(i=0;i<a.length;i++)
{
System.out.print(","+a[i]);
}
System.out.println(" The sum of those numbers is "+sum);
System.out.println("The product of those numbers is "+pro);
System.out.println("The largest number entered is"+max);
System.out.println("The smallest number entered is "+min);
System.out.println("The average of the numbers entered is "+avg);
}
}