Write a C Program to Find Largest Number Among Three Numbers.

Prince Patel
By -
0

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:
  1. Read three numbers from the user. 
  2. Compare the first number with the second and third numbers. 
  3. If the first number is greater than both the second and third numbers, it's the largest. 
  4. If not, compare the second number with the first and third numbers. 
  5. If the second number is greater than both the first and third numbers, it's the largest. 
  6. 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);
      }
}

  • 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.
Output:





Tags:

Post a Comment

0Comments

Post a Comment (0)