In: Computer Science
WRITE CODE IN JAVA
it is now your turn to create a program of your choosing. If you are not sure where to begin, think of a task that you repeat often in your major that would benefit from a program. For example, chemistry unit conversions, finding the area for geometric shapes, etc. You can also create an interactive story that changes based on the user input/decisions. The possibilities are endless.
The program must include instructions for the user. Be in the mindset that the program you develop will be used by anyone. You will not receive any assistance from the instructor. It is up to you to figure out your program.
Your program must include (but not limited to) the following:
•Comments•Input(s) and output(s)
•Decision structures
•Loops
Create whatever program of your liking
/*Make sure you hit like if solution works for you else comment in the comment section*/
I'm writing a program to find maximum, minimum, sum and avg of an array of Integers.
import java.util.Scanner;
public class MaxMinAvg {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int max=Integer.MIN_VALUE, min=Integer.MAX_VALUE;
int n,sum=0;
System.out.print("How many number you want to enter? ");
//Taking input of size of array
n=sc.nextInt();
int arr[]=new int[n];
System.out.print("Enter "+n+" numbers: ");
//Taking n inputs for array
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
for(int i=0;i<n;i++)
{
sum+=arr[i];
//Decision structure to find max and min elements of array
if(max<arr[i])
max=arr[i];
if(min>arr[i])
min=arr[i];
}
//Printing the outputs on console
System.out.println("The Minimum number is: "+min);
System.out.println("The Maximum number is: "+max);
System.out.println("The sum is: "+sum);
System.out.println("The average is: "+((float)(sum)/10));
}
}
Input:
How many number you want to enter? 5
Enter 5 numbers: 65 48 32 42 10
Output:
The Minimum number is: 10
The Maximum number is: 65
The sum is: 197
The average is: 19.7