Dremendo Tag Line

Divide larger number with smaller number in Java

if else if - Question 5

In this question, we will see how to input 2 integers and divide the larger number with the smaller one and display the result in Java programming using the if else if statement. To know more about if else if statement click on the if else if statement lesson.

Q5) Write a program in Java to input 2 integers. If either of the two number is 0, display Invalid input, if it is valid entry, divide the larger number with the smaller number and display the result.

Program

import java.util.Scanner;

public class Q5
{
    public static void main(String args[])
    {
        int a,b;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter 2 numbers");
        a=sc.nextInt();
        b=sc.nextInt();
        if(a==0 || b==0)
        {
            System.out.print("Invalid input");
        }
        else if(a>b)
        {
            System.out.print("Result of division = " + (a/(float)b));
        }
        else if(b>a)
        {
            System.out.print("Result of division = " + (b/(float)a));
        }
    }
}

Output

Enter 2 numbers
5
2
Result of division = 2.5
video-poster