This C Program finds largest number among given two integer numbers. Here two types of logics have explained.
In the first program which solve the problem using simple IF statement.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void main()
{
int n1,n2;
printf("Enter two numbers :");
scanf("%d%d", &n1,&n2);
if(n1>n2)
printf("\n Biggest among %d and %d is %d", n1, n2 ,n1);
else if (n2>n1)
printf("\n Biggest among %d and %d is %d", n1, n2, n2);
else
printf("\n Both are equal");
getch();
}
In this program, we scan two numbers into variable n1 and n2 respectively. ‘If‘ condition compares n1 and n2. If n1>n2 then variable n1 should biggest. if n2>n1 then n2 should be bigger. Else both are equal numbers. If we don’t check equality, then we don’t need else if statement, then ‘else’ is enough to ensure the second number is larger.
Second Logic:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void main()
{
int n1, n2, big;
printf("Enter two numbers :");
scanf("%d%d", &n1,&n2);
big = n1;
if(n2 > big)
big = n2;
printf("\n Biggest among %d and %d is %d", n1, n2, big);
getch();
}
This logic is better than first one. Using this logic you can find largest among three or four or any given numbers (we will see in next article).
Here another variable ‘big’ is used. First of all program suppose the biggest among given numbers in first one, that is variable n1 is assigned into the variable ‘big’. In ‘IF’ condition, if n2 is greater than ‘big’ then n2 is assigned into variable ‘big’, else value of big never changed (that is big = n1). Finally the variable ‘big’ will have the bigger value.
Sample Output:

Be First to Comment