Let’s see how to write a simple C program to Find the Largest number among three numbers. The logic is to get 3 numbers from the user and use the If condition to print out the Largest numbers among them.
#include <stdio.h>
int main()
{
int numOne, numTwo, numThree, temp, largest;
//get the number from the user
printf("Number 1:");
scanf("%d", &numOne);
printf("Number 2:");
scanf("%d", &numTwo);
printf("Number 3:");
scanf("%d", &numThree);
//use the ternary operator to
//find the largest number between numOne and numTwo and store it in temp variable
temp = (numOne > numTwo) ? numOne : numTwo;
//Now compare the temp & numThree variable to find the largest number of 3
largest = (numThree > temp) ? numThree : temp;
//print the Largest number
printf("Largest of 3 number is : %d", largest);
return 0;
}Go through the above program and read the comments to understand it. Create a new file and copy-paste the above program to execute it.
This program is useful for school/college students or those who are learning the basics of the C programming language.
From this program, you can get the knowledge of the below items:
- How to compare two Integers in C
- How to use Ternary if Condition in C (Single line If condition in C Programming)
- Store a value in temp variable in C
Hope this helps you!. All the best