When it comes to programming, one of the fundamental tasks is comparing and analyzing numbers. In this article, we’ll explore a simple C program that takes three numbers as input and identifies the greatest among them. Let’s break down the code step by step.
1. Variable Declaration
The program begins by declaring three variables of type double
– num1
, num2
, and num3
. These variables will serve as containers for the user-inputted numbers.
1 2 3 4 5 6 7 | #include <stdio.h> int main() { // Declare three variables to store the numbers double num1, num2, num3; |
2. User Input
Next, the program prompts the user to enter three numbers one by one using the printf
function. The scanf
function is then employed to read these values into our variables num1
, num2
, and num3
.
1 2 3 4 5 6 7 8 9 10 11 | // Prompt the user to enter three numbers printf("Enter the first number: "); scanf("%lf", &num1); printf("Enter the second number: "); scanf("%lf", &num2); printf("Enter the third number: "); scanf("%lf", &num3); |
3. Conditional Statements
Now comes the interesting part! The program utilizes conditional statements (if
, else if
, and else
) to compare these three numbers and determine which one is the greatest.
1 2 3 4 5 6 7 8 9 10 11 12 | // Compare the numbers to find the greatest if (num1 > num2 && num1 > num3) { printf("%.2lf is the greatest.\n", num1); } else if (num2 > num1 && num2 > num3) { printf("%.2lf is the greatest.\n", num2); } else if (num3 > num1 && num3 > num2) { printf("%.2lf is the greatest.\n", num3); } else { printf("All three numbers are equal.\n"); } |
The if
statement checks if num1
is greater than both num2
and num3
. If true, it prints a message stating that the first number is the largest. The subsequent else if
statements evaluate whether num2
or num3
is greater than the other numbers. If none of these conditions is met, the else
statement triggers, indicating that all three numbers are equal.
4. Conclusion
Finally, to conclude the program, return 0
is used to signify the end of the main()
function, indicating successful execution and bringing the program to a close.
This C program offers a glimpse into the fundamental concepts of variable declaration, user input, and conditional statements. It provides a foundation for understanding more complex programs and algorithms that involve data comparison and decision-making.
C Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | #include <stdio.h> int main() { // Declare three variables to store the numbers double num1, num2, num3; // Prompt the user to enter the first number printf("Enter the first number: "); scanf("%lf", &num1); // Prompt the user to enter the second number printf("Enter the second number: "); scanf("%lf", &num2); // Prompt the user to enter the third number printf("Enter the third number: "); scanf("%lf", &num3); // Compare the numbers to find the greatest if (num1 > num2 && num1 > num3) { printf("%.2lf is the greatest.\n", num1); } else if (num2 > num1 && num2 > num3) { printf("%.2lf is the greatest.\n", num2); } else if (num3 > num1 && num3 > num2) { printf("%.2lf is the greatest.\n", num3); } else { printf("All three numbers are equal.\n"); } // Conclude the program return 0; } |
Output:
1 2 3 4 5 6 | Enter the first number: 12.5 Enter the second number: 8.9 Enter the third number: 15.3 15.30 is the greatest. |
In this example, the user entered three numbers (12.5, 8.9, and 15.3). The program then determined that 15.30 is the greatest among the three and printed the corresponding message.