C++ program to find Largest of two numbers || comparison of two numbers || C++
In this, we are going to write a program to Find Greatest 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 greater of the two numbers

The code given below can be used for TURBO C++ Compiler:-

// to find greatest 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 greatest...\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 greater than "<<b;
    }
    else if(a < b){
        cout<<b<<" is greater than "<<a;
    }

    // if neither a is greater nor b is greater 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 greatest 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 greatest...\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 greater than "<<b;
    }
    else if(a < b){
        cout<<b<<" is greater than "<<a;
    }

    // if neither a is greater nor b is greater 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