C Program to Find the Biggest Number Among Three Numbers

 



C Program to Find the Biggest Number Among Three Numbers

Introduction

In programming, determining the largest number among a set of values is a fundamental problem often encountered by beginners. In this blog, we'll walk through a simple C program to find the biggest number among three user-provided numbers. This program is easy to understand and a great starting point for learners.

Step-by-Step Guide

1. Understand the Problem: The task is to take three numbers as input from the user and determine which number is the largest.

2. Logic: We compare the three numbers using if-else statements to find the maximum value.

C Program Code

#include <stdio.h>

int main() {
    float num1, num2, num3;

    // Prompting user for input
    printf("Enter the first number: ");
    scanf("%f", &num1);

    printf("Enter the second number: ");
    scanf("%f", &num2);

    printf("Enter the third number: ");
    scanf("%f", &num3);

    // Comparing numbers
    if (num1 >= num2 && num1 >= num3) {
        printf("The largest number is: %.2f\n", num1);
    } else if (num2 >= num1 && num2 >= num3) {
        printf("The largest number is: %.2f\n", num2);
    } else {
        printf("The largest number is: %.2f\n", num3);
    }

    return 0;
}

Explanation of the Code

  1. Input: The program prompts the user to enter three numbers.

  2. Comparison Logic: The if-else statements compare the numbers:

    • If num1 is greater than or equal to both num2 and num3, it is the largest.

    • Otherwise, if num2 is greater than or equal to the other two numbers, it is the largest.

    • If neither of the above conditions is true, num3 is the largest.

  3. Output: The program displays the largest number to the user.

Example Output

Enter the first number: 4.5
Enter the second number: 7.2
Enter the third number: 6.3
The largest number is: 7.20

Conclusion

This simple C program effectively demonstrates the use of conditional statements to solve a basic problem. As you progress, you can explore finding the largest number in larger datasets using arrays and loops.

Post a Comment

0 Comments