Dremendo Tag Line

Divide larger number with smaller number in C++

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 C++ 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 C++ 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

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int a,b;
    cout<<"Enter 2 numbers\n";
    cin>>a>>b;
    if(a==0 || b==0)
    {
        cout<<"Invalid input";
    }
    else if(a>b)
    {
        cout<<"Result of division = "<<a/(float)b;
    }
    else if(b>a)
    {
        cout<<"Result of division = "<<b/(float)a;
    }
    return 0;
}

Output

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