Experiment 2 – Largest of Three Numbers
Aim
To find the largest among three numbers entered by the user using conditional statements (if-else and nested if-else) and comparison operators in C programming. This experiment helps students understand decision-making logic, conditional statements, and comparison operations.
Through this experiment, students will learn to implement conditional logic, understand the flow of control in programs, and practice using relational and logical operators to solve real-world problems.
Learning Outcomes
After completing this experiment, students will be able to:
- Understand and implement conditional statements (if, if-else, nested if-else) in C programming
- Use relational operators (>, <, >=, <=, ==, !=) for comparison operations
- Apply logical operators (&&, ||, !) to combine multiple conditions
- Design algorithms for finding maximum/minimum values among multiple inputs
- Write clean, readable code using appropriate conditional structures
- Test programs with different input scenarios including edge cases
- Understand the concept of decision-making in programming
- Apply conditional logic to solve real-world problems
Algorithm
The algorithm to find the largest of three numbers can be implemented using different approaches. Here's a step-by-step algorithm:
Algorithm Steps:
- Start: Begin the program execution
- Input: Read three numbers from the user and store them in variables
a,b, andc - Compare: Check if
ais greater than or equal to bothbandc- If true, then
ais the largest number - If false, proceed to next comparison
- If true, then
- Compare: Check if
bis greater than or equal to bothaandc- If true, then
bis the largest number - If false, proceed to next step
- If true, then
- Default: If neither
anorbis largest, thencmust be the largest - Output: Display the largest number
- Stop: End the program
Alternative Approach: You can also use nested if-else statements. First compare a and b to find the larger one, then compare that result with c to determine the overall largest number.
Procedure
- Include
stdio.hheader file - Declare three variables to store the numbers
- Read three numbers from the user using
scanf() - Use nested if-else statements to compare the numbers:
- Compare first number with second and third
- If first is largest, display it
- Else compare second with third
- Display the largest number
- Alternatively, use logical AND operator (
&&) for cleaner code - Compile and test with different input combinations
Flowchart
START │ ├─→ Declare variables: a, b, c, largest │ ├─→ Read a, b, c │ ├─→ IF (a >= b && a >= c) │ │ │ └─→ largest = a │ ├─→ ELSE IF (b >= a && b >= c) │ │ │ └─→ largest = b │ ├─→ ELSE │ │ │ └─→ largest = c │ ├─→ Display "Largest number is: " + largest │ └─→ END
Program Code
#include <stdio.h>
int main() {
int a, b, c, largest;
// Input three numbers
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
// Find the largest using if-else
if (a >= b && a >= c) {
largest = a;
}
else if (b >= a && b >= c) {
largest = b;
}
else {
largest = c;
}
// Display result
printf("\nThe largest number among %d, %d, and %d is: %d\n",
a, b, c, largest);
return 0;
}
// Alternative method using nested if-else:
/*
if (a > b) {
if (a > c) {
largest = a;
} else {
largest = c;
}
} else {
if (b > c) {
largest = b;
} else {
largest = c;
}
}
*/Sample Input and Output
Sample 1:
Input:
Enter three numbers: 15 42 8
Output:
The largest number among 15, 42, and 8 is: 42
Sample 2:
Input:
Enter three numbers: 100 100 50
Output:
The largest number among 100, 100, and 50 is: 100
Sample 3:
Input:
Enter three numbers: 7 12 25
Output:
The largest number among 7, 12, and 25 is: 25
Use Case / Real-world Relevance
Finding the maximum or minimum value among multiple inputs is a fundamental operation in programming with numerous real-world applications:
- Grading Systems: Finding the highest score among multiple test results to determine the top performer
- Business Analytics: Identifying the best performing product, region, or salesperson in business intelligence systems
- Game Development: Determining the highest score, best time, maximum damage, or top player in leaderboards
- Data Analysis: Finding peak values, maximum temperatures, highest sales, or peak traffic in datasets
- Resource Management: Allocating resources based on maximum demand, priority, or capacity requirements
- Sorting Algorithms: This logic is fundamental to sorting and searching algorithms like selection sort
- Decision Making Systems: AI systems that need to choose the best option from multiple alternatives
- Financial Applications: Finding maximum profit, highest stock price, or best investment option
- E-commerce: Determining the highest-rated product, most popular item, or best deal among multiple options
Understanding conditional logic and comparison operations is essential for implementing decision-making in programs. This experiment forms the foundation for more complex algorithms and data structures that rely on finding maximum or minimum values.
Viva Questions
Q1: What is the difference between if and if-else statements?
The if statement executes code only when the condition is true. The if-else statement executes one block when true and another block when false, ensuring that exactly one path is taken.
Q2: What are relational operators? List them.
Relational operators compare two values and return true (1) or false (0). They include: > (greater than),< (less than), >= (greater than or equal),<= (less than or equal), == (equal to), and!= (not equal to).
Q3: What is the logical AND operator? How does it work?
The logical AND operator (&&) returns true only when both conditions are true. For example,(a >= b && a >= c) is true only if a is greater than or equal to both b and c.
Q4: Explain nested if-else statements with an example.
Nested if-else means an if-else statement inside another if-else. For example: if (a > b) { if (a > c) largest = a; else largest = c; } else { if (b > c) largest = b; else largest = c; }. This allows checking multiple conditions in a hierarchical manner.
Q5: What happens if all three numbers are equal?
If all three numbers are equal, the program will still work correctly. Using >= (greater than or equal) ensures that the first condition (a >= b && a >= c) will be true, and a will be selected as the largest (which is correct since all are equal).
Q6: Can we use switch statement for this problem? Why or why not?
No, we cannot use switch for this problem because switch works with constant values and expressions that evaluate to integers or characters. Here we need to compare variable values using relational operators, which requires conditional statements.
Q7: What is the time complexity of finding the largest of three numbers?
The time complexity is O(1) - constant time, because we perform a fixed number of comparisons (at most 2-3 comparisons) regardless of the input values. The number of operations doesn't grow with input size since we're only comparing three numbers.
Q8: How would you modify this program to find the smallest of three numbers?
To find the smallest, change all >= operators to <= operators. For example: if (a <= b && a <= c) would check if a is the smallest.
Related Links
🔹 Author: Dr. J. Siva Ramakrishna
🔹 Institution: Narayana Engineering College, Gudur
🔹 Last Updated: 9 January 2026