Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

CSC 109- Spring 2023

College of New Caledonia

Homework # 02
Name - Manpreet Singh
Student ID -0164720
Submission Date - 16-02-2023

MAIN CODE-
import java.util.Scanner;
public class Main
{
// main method begins execution of Java application
public static void main(String[] args)
{
// create Scanner to obtain input
Scanner input = new Scanner(System.in);

// initialize variables
int num1; // first integer for user input
int num2; // second integer for user input
int num3; // third integer for user input
int largest; // largest number from num1, num2, and num3
int smallest; // smallest number from num1, num2, and num3
int sum; // sum of largest and smallest
System.out.print("Enter first integer: ");
num1 = input.nextInt(); // read first integer from user

System.out.print("Enter second integer: ");


num2 = input.nextInt(); // read second integer from user
CSC 109- Spring 2023
College of New Caledonia

System.out.print("Enter third integer: ");


num3 = input.nextInt(); // read third integer from use

// Compare the numbers to find the smallest value


smallest = num1;
if (num2 <= smallest)
smallest = num2;

if (num3 <= smallest)


smallest = num3;

// Compare the numbers to find the largest value


largest = num1;
if (num2 >= largest)
largest = num2;

if (num3 >= largest)


largest = num3;
sum = smallest + largest ; // add numbers
System.out.printf("Largest number is :%d\n", largest); // display the largest
number
System.out.printf("Smallest number is: %d\n", smallest);// display the smallest
number
System.out.printf("Sum of smallest and largest numbers is: %d\n", sum);//
display the sum
} // end method main

} // end class main


CSC 109- Spring 2023
College of New Caledonia

Output-
Enter first integer: 1
Enter second integer: 2
Enter third integer: 3
Largest number is :3
Smallest number is: 1
Sum of smallest and largest numbers is: 4

You might also like