c-programs-basics
In this c program, you are going to find the greatest of two numbers, by taking the two numbers from a user as an input.

C Program To Find Which Number is Greater From Two Given Numbers

#include<stdio.h>
#include<conio.h>
void main()
{
  int a,b;
  clrscr();

  printf("\n Enter any Two numbers\n");

  printf("\n Enter First Number : ");
  scanf("%d",&a);

  printf("\n Enter Second Number : ");
  scanf("%d",&b);

  if(a>b)//logical test
   {
    printf("\n %d is Greater",a);
   }

  else
   {
    printf("\n %d is Greater",b);
   }

 getch();
}


Output :

c-program-greatest-of-two-numbers-output









Likewise we can also write a c program to find smallest of two numbers using operators.
There are several other methods to perform the same program, we will see two of them, one is by the use of a conditional operator and the second is by using arithmetical operator.

Read More  About Operators in C.

C Program To Find Greatest Of Two Numbers Using Conditional Operator/Ternary Operator

#include<stdio.h>
#include<conio.h>
void main()
{
  int a,b,c;
  clrscr();

  printf("\n Enter any Two numbers\n");

  printf("\n Enter First Number : ");
  scanf("%d",&a);

  printf("\n Enter Second Number : ");
  scanf("%d",&b);

  c=(a>b) ? a : b ;   // Conditional operator

  printf("\n %d is Greater",c);

  getch();
}

In the above program, we have used conditional / ternary operator.
  • General format of conditional operator:

( Condition ?  Statements 1 : Statements 2 )

Working : This operator works same as the If..Else.. statement, condition is declared before the question mark, if the condition is true, statements after the question mark will be executed and if the condition turns out to be false statements after the colon will be executed.

Find Greater Number From Two Using Arithmetical Operator

#include<stdio.h>
#include<conio.h>
void main()
{
  int a,b,c;
  clrscr();

  printf("\n Enter any Two numbers\n");

  printf("\n Enter First Number : ");
  scanf("%d",&a);

  printf("\n Enter Second Number : ");
  scanf("%d",&b);

  c=a-b;

  if(c>0)
  printf("\n %d is Greater",a);

  else
  printf("\n %d is Greater",b);

 getch();
}
As shown in this source code above,
  • We get two numbers from the user and subtract the second number from the first one. 
  • If the subtraction is positive (i.e greater than 0) means the first number is greater 
  • If the subtraction is negative (i.e less than 0) then the second number is greater.

C program for Dev C++ to find greater number from two given Numbers

#include<stdio.h>
int main()
{
  int a,b;
  printf("\n Enter Any Two numbers\n");
  printf("\n Enter First Number : ");
  scanf("%d",&a);
  printf("\n Enter Second Number : ");
  scanf("%d",&b);

  if(a>b)//logical test
   {
    printf("\n %d is Greater",a);
   }
  else
   {
    printf("\n %d is Greater",b);
   }
 getch();
 return 0;
}

Output:
c-program-greatest-of-two-numbers-output




We have seen how to write a C program to find the greatest number from two given numbers in various ways.