Introduction
Finding the largest of three numbers is a common programming problem that helps in understanding the use of conditional statements. This guide will show you how to write a C program to determine the largest of three numbers provided by the user.
Problem Statement
Create a C program that:
- Takes three numbers as input from the user.
- Compares the numbers to find the largest one.
- Displays the largest number.
Example:
- Input:
a = 5,b = 10,c = 7 - Output:
The largest number is 10
Solution Steps
- Include the Standard Input-Output Library: Use
#include <stdio.h>to include the standard input-output library, which is necessary for usingprintfandscanffunctions. - Write the Main Function: Define the
mainfunction, which is the entry point of every C program. - Declare Variables: Declare variables to store the three numbers.
- Input the Numbers: Use
scanfto take input from the user for the three numbers. - Compare the Numbers Using Conditional Statements: Use
if-elsestatements to compare the three numbers and determine the largest one. - Display the Largest Number: Use
printfto display the largest number.
C Program
#include <stdio.h>
/**
* C Program to Find the Largest of Three Numbers
* Author: https://www.javaguides.net/
*/
int main() {
// Step 1: Declare variables to hold the three numbers
int a, b, c;
// Step 2: Prompt the user to enter the first number
printf("Enter the first number (a): ");
scanf("%d", &a);
// Step 3: Prompt the user to enter the second number
printf("Enter the second number (b): ");
scanf("%d", &b);
// Step 4: Prompt the user to enter the third number
printf("Enter the third number (c): ");
scanf("%d", &c);
// Step 5: Compare the numbers using if-else statements
if (a >= b && a >= c) {
// If a is greater than or equal to both b and c, then a is the largest
printf("The largest number is %d\n", a);
} else if (b >= a && b >= c) {
// If b is greater than or equal to both a and c, then b is the largest
printf("The largest number is %d\n", b);
} else {
// If neither a nor b is the largest, then c must be the largest
printf("The largest number is %d\n", c);
}
return 0; // Step 6: Return 0 to indicate successful execution
}
Explanation
Step 1: Declare Variables
- Variables
a,b, andcare declared as integers to hold the three numbers entered by the user.
Step 2: Input the First Number
- The program prompts the user to enter the first number using
printf. Thescanffunction then reads the input and stores it in the variablea.
Step 3: Input the Second Number
- Similarly, the program prompts the user to enter the second number, which is stored in the variable
b.
Step 4: Input the Third Number
- The program prompts the user to enter the third number, which is stored in the variable
c.
Step 5: Compare the Numbers Using Conditional Statements
- The program uses
if-elsestatements to compare the three numbers:- If
ais greater than or equal to bothbandc, thenais the largest. - If
bis greater than or equal to bothaandc, thenbis the largest. - Otherwise,
cis the largest.
- If
Step 6: Display the Largest Number
- The program displays the largest number using the
printffunction.
Step 7: Return 0
- The
return 0;statement indicates that the program executed successfully.
Output Example
Example:
Enter the first number (a): 5
Enter the second number (b): 10
Enter the third number (c): 7
The largest number is 10
Conclusion
This C program demonstrates how to find the largest of three numbers using conditional statements. It covers basic concepts such as variable declaration, taking user input, and using if-else statements, making it a useful example for beginners learning C programming.