The process of finding the largest value is used frequently in computer applicat
ID: 670663 • Letter: T
Question
The process of finding the largest value is used frequently in computer applications. For example, a program that determines the winner of a sales contest would input the number of units sold by each sales person. The salesperson who sells the most units wins the contest. Write a pseudocode program, then a Java application that inputs a series of 10 intergers and determines and prints the largest integer. The program should use at least the following three variables:
a) counter: A counter to count to 10
b) number: the integer most recently input by the user
c) largest number found so far
Explanation / Answer
/**
* The java program FindLargest that prompts user to enter 10 values and then prints the largest values from 10 values.
* */
//FindLargest.java
import java.util.Scanner;
public class FindLargest
{
public static void main(String[] args)
{
//Create an instance of Scanner class to read input from user
Scanner scanner=new Scanner(System.in);
//Declare and initialize a counter variable to zero
//a)counter:counter variable to count to 10
int counter=0;
//b)number: the integer most recently input by the user
int number;
//c)largest number found so far
int largest;
System.out.println("Enter sales for "+(counter+1));
//read sales data for ten sales
number=scanner.nextInt();
//Assume that starting sales is the largest value
largest=number;
//for loop that runs for counter is less than 10 values
for (counter = 1; counter < 10; counter++)
{
System.out.println("Enter sales for "+(counter+1));
//read integer value from user
number=scanner.nextInt();
//check if number is greater than largest
if(number>largest)
//set number to largest
largest=number;
}
System.out.println("Largest value : "+largest);
}//end of the main method
}//end of the class FindLargest
------------------------------------------------------------------------------------------------------------------------------
Sample Output:
Enter sales for 1
150
Enter sales for 2
200
Enter sales for 3
160
Enter sales for 4
140
Enter sales for 5
180
Enter sales for 6
900
Enter sales for 7
500
Enter sales for 8
600
Enter sales for 9
550
Enter sales for 10
1000
Largest value : 1000
Hope this helps you
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.