Friday, November 30

Program To Find The Greatest Of Three Numbers



Write a C program to find the greatest of three numbers.

Program:

#include <stdio.h>
int main()
{
int a, b, c;
printf("Enter a,b,c: \n");
scanf("%d %d %d", &a, &b, &c);
if (a > b && a > c) 
      {
          printf("a is Greater than b and c");
      }
else if (b > a && b > c) 
     {
         printf("b is Greater than a and c");
      }
else if (c > a && c > b)
      {
         printf("c is Greater than a and b");
       }
else 
      {
        printf("all are equal or any two values are equal");
       }
return 0;
}

Output:
Enter a,b,c: 3 5 8
c is Greater than a and b

Explanation with examples:
Consider three numbers a=5,b=4,c=8
if(a>b && a>c) then a is greater than b and c
now check this condition for the three numbers 5,4,8 i.e.
if(5>4 && 5>8) /* 5>4 is true but 5>8 fails */
so the control shifts to else if condition
else if(b>a && b>c) then b is greater than a and c
now checking this condition for 5,4,8 i.e.
else if(4>5 && 4>8) /* both the conditions fail */
now the control shifts to the next else if condition
else if(c>a && c>b) then c is greater than a and b
now checking this condition for 5,4,8 i.e.
else if(8>5 && 8>4) /* both conditions are satisfied */
Thus c is greater than a and b.