#include <stdio.h>
int main() {
// Variable declaration
int num1, num2, num3;
// Prompt user for input
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);
// Variable to hold the largest number
int largest;
// Determine the largest number
if (num1 >= num2 && num1 >= num3) {
largest = num1; // num1 is the largest
} else if (num2 >= num1 && num2 >= num3) {
largest = num2; // num2 is the largest
} else {
largest = num3; // num3 is the largest
}
// Display the result
printf("The largest number is: %d\n", largest);
return 0;
}
Explanation
Include the Standard I/O Library:
#include <stdio.h>: This line includes the standard input-output library necessary for usingprintfandscanffunctions.
Main Function:
int main(): This is the entry point of the program.
Variable Declaration:
int num1, num2, num3;: We declare three integer variables to hold the numbers that the user will input.
User Input:
printf("Enter three numbers: ");: This line prompts the user to enter three numbers.scanf("%d %d %d", &num1, &num2, &num3);: This reads the three integers from the user and stores them innum1,num2, andnum3.
Finding the Largest Number:
- We declare an integer variable
largestto store the largest number. - The first
ifstatement checks ifnum1is greater than or equal to bothnum2andnum3. If true,num1is assigned tolargest. - The
else ifchecks ifnum2is greater than or equal to bothnum1andnum3. If true,num2is assigned tolargest. - If neither of the above conditions is true,
num3is assigned tolargest.
Display the Result:
printf("The largest number is: %d\n", largest);: This line prints out the largest number.
Return Statement:
return 0;: This indicates that the program executed successfully.
OUTPUT
Enter three numbers: 10 20 30
The largest number is: 30