Ticker

6/recent/ticker-posts

Header Ads Widget

Java program to find largest of three numbers

Java program to find largest of three numbers



Introduction
In this tutorial we will learn how to make a Java program to find largest of three numbers here we complete this tutorial into two segment.
Firstly ,we create a program in which train number will be predefined and in the second, program we will allow users to enter 3 number to find out the largest number out of three.

So lets start!! 



Theory

Here we gonna use if else statement to find the largest number of three numbers, basically what we will do is first compare the first two numbers and find the largest number out of them and then comparing it with other third number.

So let's start!!

Q) write a java program to find largest of three number
(Note :- Number will be predefined by coder)
Sol.



public class largerNumber {
public static void main(String[] args) {
    
            Double num1=4.4, num2=3.2, num3=6.0;
            
        if( num1 >= num2 && num1 >= num3)
            System.out.println(num1 + " is the largest number.");

        else if (num2 >= num1 && num2 >= num3)
            System.out.println(num2 + " is the largest number.");

        else
            System.out.println(num3 + " is the largest number.");
    

}
}




Output:

6.0 is the largest number.


Remember one thing the whole the theory and logic for the both program are same just we include the input method in the second program rest it will be the same as above program. Try to to correlate both the program to understand it easier.

Q) write a java program to find largest of three number

(Note:- Number will be entered by user)



import java.util.Scanner;
public class largerNumber {
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
    
            Double num1, num2, num3;
            System.out.println("enter number three numbers");
            num1=in.nextDouble();
              num2=in.nextDouble();
                num3=in.nextDouble();
        if( num1 >= num2 && num1 >= num3)
            System.out.println(num1 + " is the largest number.");

        else if (num2 >= num1 && num2 >= num3)
            System.out.println(num2 + " is the largest number.");

        else
            System.out.println(num3 + " is the largest number.");
    

}
}


Output:

enter number three numbers
1
2
3
3.0 is the largest number.



since in this program we allow user to enter 3 number so output will be somewhat different from the above program. Understand better correlate the both program

And because we use double here it can also encounter the point value.


Post a Comment

0 Comments