PLAY WITH CODING

Home Basic Program If & Else Loops Conversion Pattern logo

Program Based on If & Else

Compare two number whether they are equal, greater, less than the other number

Input - Output

Input
Enter a first number : 17

Enter a second number : 9

Output
17 is greater than 9

Algorithm

Step 1: START

Step 2: Take two number 'num1'and 'num2' as an input.

Step 3: Check the condition:

   if(num1>num2)

      print num1 is greater

   else if(num2>num1)

      print num2 is greater

   else

      print num1 and num2 are equal

Step 4: END

CODE


/* Compare two number */
#include  <stdio.h>
int main()
{
      int num1,num2;
      printf(" Enter first number : ");
      scanf("%d",&num1);
      printf(" Enter second number : ");
      scanf("%d",&num2);
      if(num1>num2)
          printf(" %d is greater than %d ",num1,num2);
      else if(num2>num1)
          printf(" %d is greater than %d ",num2,num1);
      else
          printf(" %d and %d is equal ",num1,num2);
    return 0;
}


/* Compare two number */
#include <iostream>
using namespace std;
int main()
{
      int num1,num2;
      cout<<" Enter first number : ";
      cin>>num1;
      cout<<" Enter second number : ";
      cin>>num2;
      if(num1>num2)
          cout<<num1<<" is greater than "<<num2;
      else if(num2>num1)
          cout<<num2<<" is greater than "<<num1;
      else
          cout<<num1<<" and "<<num2 <<" is equal ";
    return 0;
}

OutPut

Enter a first number : 17
Enter a second number : 9

17 is greater than 9