C PROGRAM TO FIND THE LARGEST NUMBER AMONG THREE NUMBERS

C Program to Find the Largest Number Among Three Numbers

C Program to Find the Largest Number Among Three Numbers

Blog Article

When working with programming, it’s common to encounter scenarios where we need to determine the largest value among three given numbers. In this article, we’ll explore a simple and efficient C program to find the largest number among three user-provided numbers. This exercise not only enhances your understanding of conditional statements but also demonstrates the power of the C language in solving practical problems.



Problem Statement


Write a program in C to accept three numbers as input and determine the largest among them. The program should work for any set of numbers, whether they are positive, negative, or zero.

Algorithm



  1. Prompt the user to input three numbers.

  2. Use conditional statements to compare the numbers.

  3. Output the largest number based on the comparison.


Code Implementation


Below is the C program to find the largest number among three numbers:
#include <stdio.h>

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

// Prompting the user for input
printf("Enter three numbers: ");
scanf("%f %f %f", &num1, &num2, &num3);

// Finding the largest number
if (num1 >= num2 && num1 >= num3) {
printf("The largest number is: %.2fn", num1);
} else if (num2 >= num1 && num2 >= num3) {
printf("The largest number is: %.2fn", num2);
} else {
printf("The largest number is: %.2fn", num3);
}

return 0;
}

Explanation of the Code



  1. Input Handling: The program uses scanf to read three floating-point numbers from the user.

  2. Comparison Logic:

    • The first if statement checks if num1 is greater than or equal to both num2 and num3.

    • The second if statement checks if num2 is greater than or equal to both num1 and num3.

    • If neither condition is true, the else block concludes that num3 is the largest.



  3. Output: The largest number is printed to the console using printf.


Sample Output


Input:
Enter three numbers: 10.5 20.3 15.8

Output:
The largest number is: 20.30

Use Cases



  • This program can be extended to find the largest number in a larger dataset using arrays and loops.

  • It is an excellent starting point for understanding decision-making constructs in C programming.


Conclusion


This simple C program to find the largest number showcases the effectiveness of using conditional statements for problem-solving. It’s a foundational concept that paves the way for tackling more complex problems in programming. Whether you are a beginner or revisiting C, this program is a great exercise to improve your logical thinking and coding skills.

Report this page