C Program to Find Largest of Two Numbers
This program takes two numbers as input and determines which one is greater using an if-else statement. It's a basic example to understand conditional logic in C.
Concept Overview
The `if-else` statement is used to compare two numbers. If the first number is greater than the second, it prints that as the largest; otherwise, the second one is displayed as the largest.
Program
C
#include <stdio.h>
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
if(num1 > num2)
printf("%d is the largest number.\n", num1);
else if(num2 > num1)
printf("%d is the largest number.\n", num2);
else
printf("Both numbers are equal.\n");
return 0;
}
Output
Enter two numbers: 25 40 40 is the largest number.
Explanation
- `if (a > b)` – Checks whether the first number is greater than the second.
- `printf()` – Displays which number is larger.
- If both numbers are equal, a message indicating equality is displayed.
Note: Note: You can also use the ternary operator `(a > b) ? a : b` for a more compact version of this program.
Codecrown