c++ program to find smallest of two numbers || comparison of two numbers || C++

In this, we are going to write a program to Find Smallest Of Two Numbers in C++.

This is a very basic program of if - case in we directly compare the two numbers input by user during runtime using inequality symbols ("<", ">") and print the obtained result which will be the smaller of the two numbers

The code given below can be used for TURBO C++ Compiler:-
// to find smallest of two integers
// and demonstrate if, else if, and else
#include<iostream.h>
#include<conio.h>

void main()
{
    clrscr();
    int a,b;
    cout<<"Enter two integers to find smallest...\n";
    cout<<"Enter first integer : \n";
    cin>>a;
    cout<<"Enter second integer : \n";
    cin>>b;

    cout<<"You entered : "<<a<<" and "<<b<<endl;
    if(a < b){
        cout<<a<<" is smaller than "<<b;
    }
    else if(a > b){
        cout<<b<<" is smaller than "<<a;
    }

    // if neither a is smaller nor b is smaller then they obviously must be equal 
    else{
        cout<<"both are equal";
    }
    getch();
}
  
    

The code given below can be used for g++/gcc Compiler:-


// to find smallest of two integers
// and demonstrate if, else if, and else
#include<iostream>
using namespace std;
int main()
{
    int a,b;  
    cout<<"Enter two integers to find smallest...\n";
    cout<<"Enter first integer : \n";
    cin>>a;
    cout<<"Enter second integer : \n";
    cin>>b;

    cout<<"You entered : "<<a<<" and "<<b<<endl;
    if(a < b){
        cout<<a<<" is smaller than "<<b;
    }
    else if(a > b){
        cout<<b<<" is smaller than "<<a;
    }

    // if neither a is smaller nor b is smaller then they obviously must be equal 
    else{
        cout<<"both are equal";
    }
    return 0;
}

  
    

#ENJOYCODING





Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post