The following program prints the greatest number among three numbers.
Program 1: C program to print the greatest of three numbers
#include<stdio.h> int main() { int a,b,c,large; printf("Enter three numbers: "); scanf("%d %d %d",&a,&b,&c); if(a>b) { if(a>c) large=a; else large=c; } else if(b>c) large=b; else large=c; printf("\nThe largest out of three numbers is: %d\n",large); return 0; }
Output: Prints largest among three numbers
Enter three numbers: 34 85 57
The largest out of three numbers is: 85
Program 2: C program to print the largest among three numbers using function
As I’ve already mentioned in other programs, it is very easy to write functions to solve a problem if you know the algorithm. Therefore, by using above algorithm, we’ll write a C program to print the greatest of three numbers using function.
#include<stdio.h> int large(int, int, int); //function prototype int main() { int a,b,c; printf("Enter three numbers: "); scanf("%d %d %d",&a,&b,&c); //printing and function calling simultaneously printf("\nThe biggest of three numbers is: %d\n",large(a, b, c)); return 0; } //function definition int large(int a, int b, int c) { // if b is smaller than a if(a>b) { if(a>c) return a; else return c; } else //if b is greater than a if(b>c) return b; else return c; }
Sample output #1:
Enter three numbers: 342 643 23
The biggest of three numbers is: 643
Sample output #2:
Enter three numbers: 93 27 132
The biggest of three numbers is: 132
That’s it. This is how we find and print the greatest among three numbers in C.
Share your code in the comments to help other beginners.