0% found this document useful (0 votes)
2 views

Lab Report 2

Lab_Report_2 (1)

Uploaded by

samirgolder2
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab Report 2

Lab_Report_2 (1)

Uploaded by

samirgolder2
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Dhaka International

University Dept of CSE

Lab Report: 02

Course Name : Object-Oriented Programming Language


Course Number : 0613-201

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 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:

• 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;

• float avg = (float) sum / 5;



• // Finding maximum value among the five numbers
• int max = Math.max(a, Math.max(b, Math.max(c, Math.max(d, e))));

• // Printing results
• System.out.println(sum);
• System.out.println(avg);
• System.out.println(max);
• }
• }

Explanation of Key Concepts:


• Scanner: Used for taking input from the console.

• 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:

• The sum of the numbers.


• The average value.
• The maximum value.

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.

You might also like