Learn how to write a C program to find the largest number among three given numbers. This tutorial walks you through the process of comparing three integers and identifying the largest one using basic conditional statements. Ideal for beginners looking to understand fundamental programming concepts in C.
#include <stdio.h>
int main() {
int num1, num2, num3;
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);
if (num1 >= num2 && num1 >= num3)
printf("%d is the largest number.\n", num1);
else if (num2 >= num1 && num2 >= num3)
printf("%d is the largest number.\n", num2);
else
printf("%d is the largest number.\n", num3);
return 0;
}
Explanation:
Inside the main() function: - Three integer variables num1, num2, and num3 are declared to store the user's input.
- The user is prompted to enter three numbers using printf.
- The scanf function reads the three numbers entered by the user.
- Conditional statements (if, else if, else) are used to compare the numbers and find the largest one. - The largest number is printed to the console using printf.