Question

MinAvg.java: Write a program that reads 10 integers and displays its minimum and average. Please consider...

MinAvg.java: Write a program that reads 10 integers and displays its minimum and average. Please consider the extreme case when all integers are positive. For instance, by given 1, 2, …, 10, the minimum will be 1.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.io.*;//imports all class from java.io package
import java.util.Scanner;//scanner class found in java.util package
public class Main
{
   public static void main(String[] args) {
      
       int arr[]= new int[10]; //integer array declaration
       int i,a,min=0; //integer variable declaration
       float avg=0,total=0; //flaot variable declaration
       System.out.println("Enter the numbers: ");
       Scanner sc = new Scanner(System.in);//Declaring sc as an object of scanner class
       for(i=0;i<10;i++)// loop for user input
       {
       System.out.println("Enter a positive integer:");
       a=sc.nextInt();// input
       if(a>0) //to consider only positive input
       {
       arr[i]=a;//positive value is stored in an array
       }
       else
       {
       i--;// else user input is taken again and loop is decremented
       System.out.println("Please enter only positive numbers!");
       }
       }
       min=arr[0]; //a value from the array is stored in a variable
       for(i=0;i<10;i++)//loop
       {
       if(arr[i]<min)//to check if it is less than min
       {
       min=arr[i]; //if it is less than min then min is changed with the new value
       }
       }
       System.out.println("The minimum is :"+min);//output
       for(i=0;i<10;i++)//loop
       {
       total=total+arr[i];//to calculate the total sum of all the array elements
       }
       avg=total/10;//average is calculated
       System.out.println("The average is:"+avg);//display of average value
   }
}
Screenshot of the code:

Screenshot of the Output:

Add a comment
Know the answer?
Add Answer to:
MinAvg.java: Write a program that reads 10 integers and displays its minimum and average. Please consider...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT