Write a C Program to Find Largest Number Among Three Numbers.
In this Article of programming, one of the fundamental tasks is to
compare and determine the largest among a set of numbers. In this article,
we'll walk through the process of writing a C program to find the largest
number among three given numbers. We'll take a step-by-step approach to
understand the logic and implementation of the program.
Program Logic
The logic behind finding the largest number among three numbers is relatively
simple. We will use conditional statements to compare the numbers and
determine the largest one. Here's the basic approach we will follow:
- Read three numbers from the user.
- Compare the first number with the second and third numbers.
- If the first number is greater than both the second and third numbers, it's the largest.
- If not, compare the second number with the first and third numbers.
- If the second number is greater than both the first and third numbers, it's the largest.
- If neither the first nor the second number is the largest, the third number is the largest.
Here's a C Program to Find Largest Number Among Three Numbers:
#include<stdio.h>
#include<conio.h>
void main()
{
int num1, num2, num3;
// Read three numbers from the user
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2,&num3);
// Compare the numbers to find the largest
if (num1 >= num2 && num1 >= num3)
{
printf("%d is the largest number\n",num1);
}
else if (num2 >= num1 && num2 >= num3)
{
printf("%d is the largest number\n",num2);
}
else
{
printf("%d is the largest number\n",num3);
}
}
#include<conio.h>
void main()
{
int num1, num2, num3;
// Read three numbers from the user
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2,&num3);
// Compare the numbers to find the largest
if (num1 >= num2 && num1 >= num3)
{
printf("%d is the largest number\n",num1);
}
else if (num2 >= num1 && num2 >= num3)
{
printf("%d is the largest number\n",num2);
}
else
{
printf("%d is the largest number\n",num3);
}
}
- In this program, we first declare three integer variables num1, num2, and num3 to store the user-input numbers. We then use the printf and scanf functions to prompt the user for input and read the three numbers.
- Next, we use a series of if and else if statements to compare the numbers and determine the largest. The conditions in the if statements check whether num1 is greater than both num2 and num3, whether num2 is greater than both num1 and num3, and if none of these conditions are met, we conclude that num3 is the largest.
- Finally, we print the result using the printf function.