#include <stdio.h>
int main()
{
// Declare variables to store the two numbers
int num1, num2;
// Ask the user for input
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
// Determine and display the maximum number
if (num1 > num2) {
printf("The maximum of %d and %d is %d\n", num1, num2, num1);
} else if (num2 > num1) {
printf("The maximum of %d and %d is %d\n", num1, num2, num2);
} else {
printf("Both numbers are equal: %d\n", num1);
}
return 0;
}
Explanation
return 0;
: This indicates that the program has executed successfully and is returning an exit status of 0
.
#include <stdio.h>
:
This line includes the standard input-output library. It allows us to use functions like printf
for output and scanf
for input.
int main()
:
This defines the main function where the program starts executing. The int
indicates that this function will return an integer.
Variable Declaration:
int num1, num2;
: Here, we declare two integer variables, num1
and num2
, to store the numbers that the user will input.
Input from the User:
printf("Enter the first number: ");
: This prompts the user to enter the first number.
scanf("%d", &num1);
: This reads the integer input from the user and stores it in num1
. The &
operator is used to refer to the address of num1
.
The same steps are repeated to get the second number, storing it in num2
.
Finding the Maximum:
The program uses an if
statement to compare the two numbers:
if (num1 > num2)
: If num1
is greater than num2
, it prints that num1
is the maximum.
else if (num2 > num1)
: If num2
is greater than num1
, it prints that num2
is the maximum.
else
: If neither condition is true, it means both numbers are equal, and it prints that they are equal.
Return Statement:
return 0;
: This indicates that the program has executed successfully and is returning an exit status of 0
.
OUTPUT
Enter the first number: 15
Enter the second number: 10
The maximum of 15 and 10 is 15
Enter the first number: 7
Enter the second number: 7
Both numbers are equal: 7