EXPERIMENT NO.
8.1 AIM:
To write a program to find the largest number in a given data set using the method larger.
8.2 PROGRAMMING EXAMPLE:
This program uses a comparison method to determine the largest number from a given set
of integers.
8.3 RESOURCES:
⮚ Java JDK 22.0.1
⮚ Java Language class
8.4 PROBLEM ANALYSIS AND ALGORITHM DESIGN
Suppose that the input data is:
15 20 7 8 28 21 43 12 35 3
Read the first number of the data set. Because this is the only number read to this point, you
can assume that it is the largest number and call it max. Next, read the second number and
call it num. Now compare max and num, and store the larger number into max. Now max
contains the larger of the first two numbers. Next, read the third number. Compare it with
max and store the larger number into max. At this point, max contains the largest of the first
three numbers. Read the next number, compare it with max, and store the larger into max.
Repeat this process for each remaining number in the data set. Eventually, max will contain
the largest number in the data set.
This discussion translates into the following algorithm:
1. Get the first number. Because this is the only number that you have read so far, it is the
largest number so far. Save it in a variable called max.
2. For each remaining number in the list:
a. Get the next number. Store it in a variable called num.
b. Compare num and max. If max < num, then num is the new largest number; update the
value of max by copying num into max. If max >= num, discard num; that is, do nothing.
3. Because max now contains the largest number, print it.
To find the larger of two numbers, the program uses the method larger.