C Program to Find the Largest of Three Numbers

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

  1. Include the Standard Input-Output Library: Use #include <stdio.h> to include the standard input-output library, which is necessary for using printf and scanf functions.
  2. Write the Main Function: Define the main function, which is the entry point of every C program.
  3. Declare Variables: Declare variables to store the three numbers.
  4. Input the Numbers: Use scanf to take input from the user for the three numbers.
  5. Compare the Numbers Using Conditional Statements: Use if-else statements to compare the three numbers and determine the largest one.
  6. Display the Largest Number: Use printf to 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, and c are 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. The scanf function then reads the input and stores it in the variable a.

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-else statements to compare the three numbers:
    • If a is greater than or equal to both b and c, then a is the largest.
    • If b is greater than or equal to both a and c, then b is the largest.
    • Otherwise, c is the largest.

Step 6: Display the Largest Number

  • The program displays the largest number using the printf function.

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.

Leave a Comment

Scroll to Top