Java if statement Question 5

Question

We would like to find the largest and smallest Integers.

Write an application that reads five integers.

Determine and print the largest and smallest integers in the group.

Use if statements

import java.util.Scanner;

public class Main {

  public static void main(String[] args) {
    /*from   www .  j  ava2s .  co  m*/
    //your code here
  }

}


import java.util.Scanner;

public class Main {

  public static void main(String[] args) {
    
    Scanner input = new Scanner(System.in);
    int a, b, c, d, e;
    
    System.out.println("Enter 5 integers:");
    a = input.nextInt();
    b = input.nextInt();
    c = input.nextInt();
    d = input.nextInt();
    e = input.nextInt();
    
    int smallest = a;
    int largest = a;
    
    // Finding the smallest number given by user
    if (b < a)
      smallest = b;
    if (c < b)
      smallest = c;
    if (d < c)
      smallest = d;
    if (e < d)
      smallest = e;
    
    // Finding the largest number given by user
    if (b > a)
      largest = b;
    if (c > b)
      largest = c;
    if (d > c)
      largest = d;
    if (e > d)
      largest = e;
    
    System.out.printf("The smallest number is %d and the largest is %d", smallest, largest);
    
    input.close();
  }

}



PreviousNext

Related