Wednesday, September 9, 2015

Java program to find largest of three numbers

This java program finds largest of three numbers and then prints it. If the entered numbers are unequal then "numbers are not distinct" is printed.

Java programming source code

import java.util.Scanner;
class LargestOfThreeNumbers { 
public static void main(String args[]) { 
int x, y, z; 
System.out.println("Enter three integers "); 
Scanner in = new Scanner(System.in);   
x = in.nextInt(); 
y = in.nextInt(); 
z = in.nextInt();   
if ( x > y && x > z ) 
System.out.println("First number is largest."); 
else if ( y > x && y > z ) 
System.out.println("Second number is largest."); 
else if ( z > x && z > y ) 
System.out.println("Third number is largest."); 
else System.out.println("Entered numbers are not distinct."); 
} 
}

Output of program:


If you want to find out largest of a list of numbers say 10 integers then using above approach is not easy, instead you can use array data structure.

Related Posts:

  • Java program print prime numbers This java program prints prime numbers, number of prime numbers required is asked from the user. Remember that smallest prime number is 2. … Read More
  • Java program to convert Fahrenheit to Celsius Java program to convert Fahrenheit to Celsius: This code does temperature conversion from Fahrenheit scale to Celsius scale. Java programming code… Read More
  • Enhanced for loop java Enhanced for loop java: Enhanced for loop is useful when scanning the array instead of using for loop. Syntax of enhanced for loop is: for (dat… Read More
  • Java if else Program Java if else program uses if else to execute statement(s) when a condition is fulfilled. Below is a simple program which explains the usage of if els… Read More
  • Java for loop Java for loop used to repeat execution of statement(s) until a certain condition holds true. for is a keyword in Java programming language. Java … Read More

0 Comments:

Post a Comment