C Program to find greatest of three numbers

In this program, We take three numbers by user and print the greatest number among them. We simply use if statement to find the greatest number among them.

C Program to find the greatest of three numbers 

In this program, We take three numbers by the user and print the greatest number among them. We simply use if statement to find the greatest number among them.


Source Code:


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr(); 
printf("Enter three numbers:");
 scanf ("%d%d%d",&a ,&b,&c);
 if (a>b&&a>c)
{
printf("%d", a);
printf("is greatest");
printf("among the three numbers");
}
else if (b>a&&b>c) 
{
printf("%d",b);
printf("is greatest");
printf("among the three numbers");
else {printf("%d",c);
printf ("is greatest");
printf("among the three numbers");
}
getch ();
}



Output:-

C Program to find greatest of three numbers


Algorithm to find the greatest number among three numbers: 

  • Initialize three int variables.
  • Input these three numbers by the user using scanf.
  • Now we compare the first and second numbers and First and third numbers using the if statement. If the first number is greater than the second number then it will check for the first number and the third number, if it is also true then we will print the first number is the largest number.

     18>7 && 18>4

      1       && 1

      The output will be 1, which means the condition is true.

  • Same we will do with the second number
  • If it finds "condition is true" then it will print second number is the largest number.
  • Otherwise, we will print the third number is the largest number among them.


And operator:

In this program, we have used AND operator. Let suppose, we have a statement containing two conditions and we have used AND operator between these conditions. Then, it will be true if both conditions are true. If one of the conditions or both conditions are false then the statement will be false



Thanks for reading C Program to find the greatest of three numbers. Although it is a simple program, if you have any queries related to this program you can comment below.

You may also like

Post a Comment

Please do not enter any spam link in the comment box.