C Program To Print 3 Numbers In Descending Order Using If Else
In this article, you will learn how to write a C program that takes three numbers as input and prints them in descending order using if-else statements. This skill is fundamental for understanding conditional logic in programming.
Problem Statement
A common task in programming is to sort data. When dealing with a small, fixed number of elements, direct comparison using conditional statements is an efficient way to achieve this. The problem at hand is to accept three distinct integer inputs from a user and display them from the largest to the smallest. This requires a systematic approach to compare all possible permutations of the numbers.
Example
Consider the following input: First number: 10 Second number: 50 Third number: 30
The desired output, in descending order, would be: 50, 30, 10
Background & Knowledge Prerequisites
To understand this program, readers should have a basic grasp of:
- C Language Basics: Understanding variables, data types (especially
int), and input/output operations (printf,scanf). - Conditional Statements: Familiarity with
if,else if, andelseconstructs and how they control program flow based on conditions. - Comparison Operators: Knowledge of operators like
>,<,>=,<=,==,!=.
Use Cases or Case Studies
Ordering numbers is a fundamental operation with various practical applications:
- Ranking Systems: Displaying top scores in a game or academic grades in descending order.
- Financial Analysis: Identifying the highest, middle, and lowest performing assets from a small set.
- Resource Allocation: Prioritizing tasks or resources based on a numerical value (e.g., urgency, cost).
- Data Validation: Ensuring numbers fall within a certain range or order before further processing.
- Simple Decision Making: In embedded systems or microcontrollers, comparing sensor readings to trigger actions based on thresholds.
Solution Approaches
For sorting three numbers, we can use a series of if-else statements to cover all possible orderings. This approach directly compares the numbers to determine their relative positions.
Using Nested if-else for Direct Comparison
This method involves a series of nested if-else statements to determine the largest, then the next largest, and finally the smallest number.
One-line summary: Compare the three numbers using nested if-else structures to explicitly find and print them in descending order.
// Sort Three Numbers in Descending Order
#include <stdio.h>
int main() {
int num1, num2, num3;
// Step 1: Prompt user for input
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
printf("Enter the third number: ");
scanf("%d", &num3);
// Step 2: Determine the order using if-else statements
printf("Numbers in descending order: ");
if (num1 >= num2 && num1 >= num3) {
printf("%d, ", num1);
if (num2 >= num3) {
printf("%d, %d\\n", num2, num3);
} else {
printf("%d, %d\\n", num3, num2);
}
} else if (num2 >= num1 && num2 >= num3) {
printf("%d, ", num2);
if (num1 >= num3) {
printf("%d, %d\\n", num1, num3);
} else {
printf("%d, %d\\n", num3, num1);
}
} else { // num3 must be the largest
printf("%d, ", num3);
if (num1 >= num2) {
printf("%d, %d\\n", num1, num2);
} else {
printf("%d, %d\\n", num2, num1);
}
}
return 0;
}
Sample Output:
Enter the first number: 45
Enter the second number: 12
Enter the third number: 78
Numbers in descending order: 78, 45, 12
Stepwise Explanation:
- Input: The program starts by declaring three integer variables (
num1,num2,num3) and then prompts the user to enter values for each usingprintfandscanf. - First Largest Check:
- The outermost
if (num1 >= num2 && num1 >= num3)checks ifnum1is the largest among all three. - If
num1is indeed the largest, it is printed first. - Inside this block, a nested
if (num2 >= num3)then comparesnum2andnum3to determine their order, printing the larger one next, followed by the smallest.
- Second Largest Check:
- If
num1was not the largest, the program proceeds toelse if (num2 >= num1 && num2 >= num3). This checks ifnum2is the largest. - If
num2is the largest, it's printed first. - A nested
if (num1 >= num3)then ordersnum1andnum3.
- Third Largest (Else Block):
- If neither
num1nornum2was the largest, it implicitly meansnum3must be the largest. This logic is handled by the finalelseblock. -
num3is printed first. - A nested
if (num1 >= num2)then orders the remaining two numbers,num1andnum2.
- Output: In each case, the numbers are printed in descending order, separated by commas, followed by a newline character.
Conclusion
Using if-else statements provides a clear and direct way to sort a small, fixed number of elements like three integers. While this method can become complex for more numbers, it effectively illustrates fundamental conditional logic. This program effectively identifies the largest number first, then the next largest, and finally the smallest, ensuring the output is always in descending order.
Summary
- Sorting three numbers using
if-elseinvolves comparing all possible relationships between them. - Nested
if-elsestructures allow for determining the largest, middle, and smallest numbers sequentially. - The approach first finds the maximum, then compares the remaining two to find their order.
- This method is suitable for a fixed, small number of items and demonstrates basic conditional programming.