In our previous program for finding biggest among two given number we studied the logic of finding biggest among numbers.
Here using the same logic, we can find the biggest among given three and four numbers. Using this logic you can find biggest among any number of numbers. This logic is the logic of sorting numbers.
Program code for getting biggest among given three numbers:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void main()
{
int n1,n2,n3,big;
printf("Enter three numbers :");
scanf("%d%d%d", &n1,&n2,&n3);
big = n1;
if(n2 > big)
big = n2;
if (n3> big)
big = n3;
printf("\n Biggest among %d,%d and %d is %d", n1, n2, n3, big);
getch();
}
As we shown in previous program, first of all we consider the first number n1 is bigger than others, so we assign n1 into variable ‘big’.
Then we check every other two variable with this ‘big’ variable. If n2 is greater than big (n2>big) then value of big is changed into n2, if not, then value of big is still n1. And if the value of n3 greater than big (may be n1 or n2), then variable ‘big’ gets the value of n3, if not, then the variable ‘big’ contains biggest among n1 and n2 that we confirmed already.
Sample Output:

Program code for getting biggest among given four numbers:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void main()
{
int n1,n2,n3,n4,big;
printf("Enter four numbers :");
scanf("%d%d%d%d",&n1,&n2,&n3,&n4);
big = n1;
if(n2 > big)
big = n2;
if (n3> big)
big = n3;
if (n4> big)
big = n4;
printf("\n Biggest among given numbers : %d", big);
getch();
}
This logic is similar to above program. Here we compare each number with the variable ‘big’.
Sample Output:

Be First to Comment