Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Java Program to find Largest of 3 numbers

In this post, we are going to see how to write a Simple Java Program to find the Largest of 3 numbers. This program is very much useful for School and College students those who are learning the basics of Java Programming. Let’s not waste the time here and see how we can find the Largest numbers.

First, we need to get the 3 numbers from the users. So we can use Scanner() class to get the input from the user and then write our if conditions to find the largest among three numbers.

// Online Java Compiler
// Use this editor to write, compile and run your Java code online

import java.util.Scanner; 

class HelloWorld {
    
    public static void main(String[] args) {
        
        //Declare our integer variables
        int numOne, numTwo, numThree, temp, largest;
        
        //Create a Scanner object to get the inputs from the user
        Scanner myObj = new Scanner(System.in);
        
        //get the number from the user
        System.out.print("Number 1:");
        numOne = myObj.nextInt(); //we have declared integers so nextInt() should be used
        
        System.out.print("Number 2:");
        numTwo = myObj.nextInt();
        
        System.out.print("Number 3:");
        numThree = myObj.nextInt();
    
        //use the ternary operator to 
        //find the largest number between numOne and numTwo and store it in temp variable 
        temp = (numOne > numTwo) ? numOne : numTwo;
        
        //Now compare the temp & numThree variable to find the largest number of 3 
        largest = (numThree > temp) ? numThree : temp;
        
        //print the Largest number
        System.out.println("Largest number is " + largest);
        
    }
}

Read the comments from the above lines of code to understand its functionality. It’s a pretty straightforward program. I hope this helped you learn something new today.

The post Java Program to find Largest of 3 numbers appeared first on TutorialsMade.



This post first appeared on TutorialsMade - Ultimate Tutorial, please read the originial post: here

Share the post

Java Program to find Largest of 3 numbers

×

Subscribe to Tutorialsmade - Ultimate Tutorial

Get updates delivered right to your inbox!

Thank you for your subscription

×