Lab Report 2
Lab Report 2
Lab Report: 02
Submitted By :
Sudip Mandal
Batch D-85
Roll 05
HackerRank Profile : https://www.hackerrank.com/profile/sudip7981
Submitted To :
Majharul Hasan Lecturer,
Dept of Computer Science & Engineering
Dhaka International University
Introduction:
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:
The program is structured as follows:
• Imports: The Scanner class from the java.util package is imported to allow user
input.
• 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 results for the sum, average, and maximum value are printed to the
console using System.out.println().
Code:
• import java.util.Scanner;
•
• public class Shuvo420 {
• public static void main(String[] args) {
• Scanner scan = new Scanner(System.in);
•
• int a = scan.nextInt();
• int b = scan.nextInt();
• int c = scan.nextInt();
• int d = scan.nextInt();
• int e = scan.nextInt();
•
• int sum = a + b + c + d + e;
• Math.max(): A built-in Java method that returns the greater of two numbers. It is
used in a nested manner to compare multiple numbers.
• Type Casting (float): Casting the sum to a float ensures that the division
result will include decimal places if necessary.
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.