C Program to Find Largest of Three numbers

In C Programming, there are many approaches to find the largest number of the three numbers. Let me show you how to write a C program to find largest of three numbers using Else If Statement, Nested If, and Conditional Operator.

C Program to find Largest of Three numbers using Else If Statement

This program helps the user to enter three different values. Then this program finds the largest number among three numbers using the Else If Statement.

#include <stdio.h>

int main()
{
 int a, b, c;

 printf("Please Enter three different values\n");
 scanf("%d %d %d", &a, &b, &c);
 
 if (a > b && a > c) 
  {
   printf("\n%d is Highest Among both %d and %d", a, b, c); 
  }
 else if (b > a && b > c) 
  {
   printf("\n%d is Highest Among both %d and %d", b, a, c);
  }
 else if (c > a && c > b) 
  {
   printf("\n%d is Highest Among both %d and %d", c, a, b);
  }
 else 
  {
   printf("\nEither any two values or all the three values are equal");
  } 
 return 0;
}

ANALYSIS of the Else If Statement

  1. In this example, the first if condition checks whether a is greater than b and a is greater than c. if this condition is True, a is highest among both b, c.
  2. The first Else if condition check whether b is greater than a and b is greater than c. If this condition is True, b is highest among both a, c.
  3. The second Else if condition check whether c is greater than a, and c is greater than b. If this condition is True, c is highest among both a, b.
  4. If all the above conditions fail, it means they are equal.

Output 1

Please Enter three different values
12
4
6

12 is Highest Among both 4 and 6

OUTPUT 2:

Please Enter three different values
19
25
20

25 is Highest Among both 19 and 20

3rd OUTPUT:

Please Enter three different values
45
36
96

96 is Highest Among both 45 and 36

Let’s enter 5 as all three numbers

Please Enter three different values
5
5
5

Either any two values or all the three values are equal

C Program to find Largest of Three numbers using Nested If Statement

This program helps the user to enter three different values. Next, this program finds the Largest of Three numbers using Nested If.

#include<stdio.h>

int main()
{
 int a, b, c;

 printf("Please Enter three different values\n");
 scanf("%d %d %d", &a, &b, &c);

 if(a-b>0 && a-c>0)
  {
    printf("\n%d is Greater Than both %d and %d", a, b, c); 
  } 
 else
  { 
   if(b-c>0)
    {
     printf("\n%d is Greater Than both %d and %d", b, a, c);
    } 
   else
    {
     printf("\n%d is Greater than both %d and %d", c, a, b);
    }
  }
 return 0;
}

In this C program to find the largest of three numbers example

  1. First, if condition checks whether a-b and a-c is greater than 0. If we subtract a small number from a big number, then this condition fails. Otherwise, it will be True. If this condition is True, then a is greater than both b, c.
  2. Else statement will execute when the first If condition is False, so there is no need to check for a value. In the C Programming Else statement, we are inserting one more if condition (Nested IF) to check whether b-c is greater than 0. If this condition is True, then b is greater than both a, c.
  3. Else c is highest among both a, b.

OUTPUT 1: Lets enter the values a = 32, b = 45, c= 98

C Program to Find Largest of Three numbers using Nested If Statement 3a

Lets enter the values a = 22, b = 5, c= 7

Please Enter three different values
22
5
7

22 is Greater Than both 5 and 7

OUTPUT 3:

Please Enter three different values
56
222
98

222 is Greater Than both 56 and 98

C Program to find Largest of Three numbers using Conditional Operator

This program helps the user to enter three different values. Next, this Program find the largest among three numbers using Conditional Operator

#include<stdio.h>

int main()
{
 int a, b, c, largest;

 printf("Please Enter three different values\n");
 scanf("%d %d %d", &a, &b, &c);
 
 largest =((a>b && a>c)?a: (b>c)?b:c);

 printf("\nLargest : %d", largest);
 return 0;
}

Within this example

  1. We declared an integer variable called the largest, and we assigned this variable to the conditional operator functionality.
  2. The first condition checks whether a is greater than b and a is greater than c. If this condition is True, it returns the first value after the ? symbol, which is variable a (a is greater than both b, c).
  3. If the first condition fails, it executes the variable after the : symbol. By using the Nested conditional operator, we are checking one more condition (b>c). If this condition is True, it returns the first value after the ? symbol, which is variable b (b is greater than both a, c.
  4. If the Nested condition fails, returns the variable after the : symbol, which is variable c. It means c is greater than both a, b.

OUTPUT 1: Lets enter the values a= 56, b= 9, c= 26

Please Enter three different values
56
9
26

Largest : 56

output 2:

Please Enter three different values
12
48
39

Largest : 48

a= 2, b= 56, c=98

Please Enter three different values
2
56
98

Largest : 98

Comments are closed.