PhysicsTeacher.in

High School Physics + more

Java Program to find the greatest and smallest numbers

Write a Java program to accept 10 different numbers and display the greatest and smallest numbers from the set of numbers entered by the user.

Find the greatest and smallest numbers – Java Program

import java.util.Scanner;
public class HighestLowest

{
public static void main(String[] args) {
int num,i;

int highest=0,lowest=0;
Scanner s = new Scanner(System.in);
System.out.println(“Enter 10 different numbers:”);
for(i=1;i<=10;i++)

{

num = s.nextInt();

if (i==1)

{highest=lowest=num;}

else

{

if (num>highest)
highest=num;
else if(num<lowest)
lowest=num;
}
}

System.out.println(“highest:”+highest);

System.out.println(“lowest:”+lowest);
}
}

See also  Java Program - how to use DataInputStream
Scroll to top
error: physicsTeacher.in