My Bookmarks

  • No bookmark found

Most Active Members

Write a program to find maximum of three numbers in Java1 min read

AIM: – Write a program to find maximum of three numbers.

Here we are writing about Write a program to find maximum of three numbers in Java. Our initiative is that user understand code by reading comment so that he/she can became capable to any understand code in this programming world.

Source Code: –

import java.util.Scanner;

public class MaxOfThreeNumbers {

    public static void main(String[] args) {

        int firstNumber,secondNumber,thirdNumber, maxNumber;
        Scanner scanner = new Scanner(System.in);
        
        //Reading three numbers from the console
        System.out.print("Enter the three numbers:- ");
        firstNumber = scanner.nextInt();
        secondNumber = scanner.nextInt();
        thirdNumber = scanner.nextInt();
        
        /*using if-else ladder
        * If first number is greater than second number then it is checked if it is greater than third.
        * If yes then first number is maximum, otherwise third number is maximum.
        * If first number is not greater than second number then it is checked if second number is
        * greater than third number. If yes then second number is maximum, otherwise third
        * number is maximum.
        */
        if(firstNumber > secondNumber) {
            if(firstNumber > thirdNumber) {
                maxNumber = firstNumber;
            } else {
                maxNumber = thirdNumber;
            }
        } else {
            if(secondNumber > thirdNumber) {
                maxNumber = secondNumber;
            } else {
                maxNumber = thirdNumber;
            }
        }
        System.out.println("Maximum of three numbers is " + maxNumber);
    }
}

Output:

Bookmark(0)
Tags:

Add a Comment

Your email address will not be published. Required fields are marked *