Write a Java program to input three integer numbers and find out the
greatest and smallest number.
Answers
Answer:
import java.util.Scanner;
public class Exercise3 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Input the 1st number: ");
int num1 = in.nextInt();
System.out.print("Input the 2nd number: ");
int num2 = in.nextInt();
System.out.print("Input the 3rd number: ");
int num3 = in.nextInt();
if (num1 > num2)
if (num1 > num3)
System.out.println("The greatest: " + num1);
if (num2 > num1)
if (num2 > num3)
System.out.println("The greatest: " + num2);
if (num3 > num1)
if (num3 > num2)
System.out.println("The greatest: " + num3);
}
}
Answer:
import java.util.*;
public class Question {
public static void main(String[] agrs) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int g = Math.max(a,Math.max(b,c));
int s = Math.min(a,Math.min(b,c));
System.out.printn("Greatest Number = "+g);
System.out.printn("Smallest Number = "+s);
}
}