C

Finding the Greatest Among Three Numbers in C: A Step-by-Step Guide3 min read

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 doublenum1, num2, and num3. These variables will serve as containers for the user-inputted numbers.

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.

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.

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:

Output:

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.

Leave a Comment