Lab Report 2
Lab Report 2
Dept of CSE
Lab Report - 2
Submitted By
Nasir Ahammad Suvo
Batch D-85
Roll 39
Submitted To
In this lab, we aim to develop a simple Java program that demonstrates the use of basic
input/output operations, arithmetic calculations, and comparison functions. The program
takes five integer inputs from the user, calculates the sum and average of these values, and
determines the largest number among them using Java’s built-in functions.
Objective:
To write a Java program that takes five integers as input, calculates their sum, average,
and determines the maximum value among them.
Code Overview:
1. Imports: The Scanner class from the java.util package is imported to allow user input.
2. Main Method:
• A Scanner object is created to take input from the user.
• Five integers (a, b, c, d, e) are read from the console using nextInt() method of the
Scanner.
• The sum of the five integers is calculated.
• The average is calculated by dividing the sum by 5. To avoid integer division, the
sum is cast to float.
• The maximum value is determined using the Math.max() method. It compares all
five numbers using nested Math.max() calls.
• The results for the sum, average, and maximum value are printed to the console
using System.out.println().
Code:
1. import java.util.Scanner;
2.
3. public class Shuvo420 {
4. public static void main(String[] args) {
5. Scanner scan = new Scanner(System.in);
6.
7. int a = scan.nextInt();
8. int b = scan.nextInt();
9. int c = scan.nextInt();
10. int d = scan.nextInt();
11. int e = scan.nextInt();
12.
13. int sum = a + b + c + d + e;
14. float avg = (float) sum / 5;
15.
16. // Finding maximum value among the five numbers
17. int max = Math.max(a, Math.max(b, Math.max(c, Math.max(d, e))));
18.
19. // Printing results
20. System.out.println(sum);
21. System.out.println(avg);
22. System.out.println(max);
23. }
24. }
Explanation of Key Concepts:
Execution:
When this code runs, it prompts the user to input five integers. After entering the numbers,
it displays:
Sample Input/Output:
• Input:
➢ 15
➢ 20
➢ 14
➢ 18
➢ 12
• Output:
➢ 79
➢ 15.8
➢ 20
Conclusion:
The program successfully takes five integer inputs from the user and calculates the sum,
average, and maximum of those numbers. The nested Math.max() function is an efficient way
to determine the largest of the five numbers. This solution is simple and demonstrates basic
Java input/output operations, arithmetic calculations, and comparison functions.