Write Java Program to Find the Largest Among Three Numbers

Java Program to Find the Largest Among Three Numbers

Welcome to the World of Online Learning:

Hello Friends “This blog helps you to learn Java programming concepts. You can learn Java  language at your own speed and time. One can learn concepts of Java language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

 Java Program to Find the Largest Among Three Numbers
Java Program to Find the Largest Among Three Numbers

Write Java Program to Find the Largest Among Three Numbers

PROGRAM: Java Program to Find the Largest Among Three Numbers

/* Java Program to Find the Largest Among Three Numbers */
1. Find Largest Among three numbers using if..else statement

public class Largest {

public static void main(String[] args) {

double n1 = -4.5, n2 = 3.9, n3 = 2.5;

if( n1 >= n2 && n1 >= n3)
System.out.println(n1 + ” is the largest number.”);

else if (n2 >= n1 && n2 >= n3)
System.out.println(n2 + ” is the largest number.”);

else
System.out.println(n3 + ” is the largest number.”);
}
}

2.Find the largest number among three using nested if..else statement

public class Largest {

public static void main(String[] args) {

double n1 = -4.5, n2 = 3.9, n3 = 5.5;

if(n1 >= n2) {
if(n1 >= n3)
System.out.println(n1 + ” is the largest number.”);
else
System.out.println(n3 + ” is the largest number.”);
} else {
if(n2 >= n3)
System.out.println(n2 + ” is the largest number.”);
else
System.out.println(n3 + ” is the largest number.”);
}
}
}

 

Leave a Reply

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