168k views
4 votes
Write a class AnalyzeNumbers which asks the user to enter the number of inputs andstores them in an Array, then prints the Array that was inputted, calculates the sum ofthe numbers entered and prints the total numbers greater than the first inputtednumber: Two Sample RunsEnter the number of items: 5Enter the numbers: 3.4 5 6 7.1 9You inputted: 3.4 5.0 6.0 7.1 9.0Sum is: 30.5Number of elements greater than the first element: 4Enter the number of items: 3Enter the numbers: 6.7 1.5 3.2You inputted: 6.7 1.5 3.2Sum is: 11.4Number of elements greater than the first element: 0

User Westley
by
7.5k points

1 Answer

2 votes

Answer:

The program to this question can be given as:

Program:

import java.util.*; //import package for user input.

class AnalyzeNumbers //defining class

{

public static void main(String ar[]) //defining main method

{

//defining variables

int i,number,count=0;

double total= 0,val;

Scanner ob = new Scanner(System.in); //creating Scanner class object.

System.out.print("Enter the number of element you want to insert:"); //message.

number = ob.nextInt(); //taking input

double a[] = new double[number]; //defining array.

System.out.println(" insert numbers :"); //message

for(i=0;i<number;i++) //loop to calculate sum

{

a[i] = ob.nextDouble(); //input array element

total=total+a[i]; //add number.

}

System.out.print("You inputted:"); //message

for(i=0;i<number;i++) //loop for print array elements

{

System.out.print(" "+ a[i]); //print value.

}

System.out.println();

val = Math.round(total* 100.0)/100.0; //val calculate value that is given in floating point

System.out.println("Sum is: "+val); //print value

for(i=0;i<number;i++) //loop for check condition.

{

if(a[i]>a[0])

{

count++; //increment value of count variable.

}

}

System.out.println("Number of elements greater than the first element:"+count); //print value.

}

}

Output:

Enter the number of element you want to insert:5

insert numbers :

3.4

5

6

7.1

9

You inputted: 3.4 5.0 6.0 7.1 9.0

Sum is: 30.5

Number of elements greater than the first element:4

Step-by-step explanation:

  • In the above java program firstly a package is import for user input then a class "AnalyzeNumbers" is defined that contains the main method in this method variables is defined that are " i, number, count, total, and val ". in which variable i, number, and count is an integer variable and total, val is double type.
  • After defining a variable we create a scanner class object that is "ob" and use number variable for user input.
  • In this method three for loop is used the first loop is used array variable a[] and scanner class object for taking multiple values from the user and calculate the sum of these values.
  • The second loop is used for print user input value. In the next line, a double variable val is used to convert all value in floating-point.
  • In the third loop, a conditional statement is used that includes all values in a set and increment count variable value by 1 and prints its value.
User Cooshal
by
6.9k points