Java program for second lowest of 3 numbers.

Welcome to the world of computer programming! In this article, we will discuss how to write a program in Java to accept three numbers and display the second lowest number. We will look at the different methods and techniques that can be used to accomplish this task.

#Method 1

import java.util.*;

public class SecondLowest 
{
  public static void main(String[] args) 
  {
    Scanner in = new Scanner(System.in);
    int a,b,c,sl;
    System.out.print("Enter three numbers: ");
    a = in.nextInt();
    b = in.nextInt();
    c = in.nextInt();
    
    sl = Math.min(Math.max(a, b), Math.max(b, c));
    System.out.println("The second lowest number is: " + sl);
  }
}

Output:

Enter three numbers: 
9
6
3
The second lowest number is: 6

#Method 2

import java.util.*;

public class SecondLowest 
{
    public static void main(String[] args) 
    {
        Scanner in = new Scanner(System.in);
        int a,b,c,sl;
        System.out.print("Enter three numbers: ");
        a = in.nextInt();
        b = in.nextInt();
        c = in.nextInt();
        
        if (a < b && a < c) 
        {
            System.out.println("The second lowest number is: " + Math.min(b, c));
        } 
        else if (b < a && b < c) 
        {
            System.out.println("The second lowest number is: " + Math.min(a, c));
        } 
        else 
        {
            System.out.println("The second lowest number is: " + Math.min(a, b));
        }
    }
}

#Method 3

import java.util.*;

public class SecondLowest 
{
    public static void main(String[] args) 
    {
        Scanner in = new Scanner(System.in);
        int a,b,c,sl;
        System.out.print("Enter three numbers: ");
        a = in.nextInt();
        b = in.nextInt();
        c = in.nextInt();
        
        if (a < b && a < c) 
        {
            if(b<c)
            System.out.println("The second lowest number is: " +b );
            else
            System.out.println("The second lowest number is: " +c );
        } 
        else if (b < a && b < c) 
        {
            if(a<c)
            System.out.println("The second lowest number is: " +a );
            else
            System.out.println("The second lowest number is: " +c );
        } 
        else 
        {
            if(a<b)
            System.out.println("The second lowest number is: " +a );
            else
            System.out.println("The second lowest number is: " +b );
        }
    }
}

Explain:

First, it imports the java.util.* class, which is used to get input from the user.

In the main method, it creates a Scanner object named in to get input from the user. Then, it declares three integer variables a, b, and c to store the three numbers entered by the user.

The program then prompts the user to enter three numbers, and stores their input in the a, b, and c variables using the nextInt method of the Scanner class.

The program uses a series of statements to determine which number is the lowest, and which number is the second lowest. If a is the lowest, then the second lowest number is the minimum of b and c. If b is the lowest, then the second lowest number is the minimum of a and c. If c is the lowest, then the second lowest number is the minimum of a and b.

The second lowest number is found using the Math.min function, which takes two numbers as arguments and returns the minimum of the two.

Finally, the second lowest number is displayed using the System.out.println function.

Conclusion

In conclusion, this program written in Java is a great tool for quickly finding the second lowest number amongst a set of three numbers. This program can be easily adapted and modified to accept more than three numbers, making it a versatile tool for any number-related task. With this program, you can easily find the second lowest number from any set of numbers, making it an invaluable asset for anyone who needs to quickly find the second lowest number.

Share your love