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 the 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 C program find the largest number among three numbers using Else If Statement in C

/* C Program to find Largest of Three numbers using 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 Greater Than both %d and %d", a, b, c); 
  }
 else if (b > a && b > c) 
  {
   printf("\n%d is Greater Than both %d and %d", b, a, c);
  }
 else if (c > a && c > b) 
  {
   printf("\n%d is Greater Than 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 C Else If Statement

  1. In this greatest of three numbers in c example, First if condition checks whether a is greater than b and a is greater than c. if this condition is True, a is greater than 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 greater than 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 greater than both a, b.
  4. If all the above conditions fail, it means they are equal.

OUTPUT 1: Lets enter the values a = 12, b = 4, c= 6

Please Enter three different values
12
4
6

12 is Greater Than both 4 and 6

OUTPUT 2: Lets enter the values a = 19, b = 25, c= 20

Please Enter three different values
19
25
20

25 is Greater Than both 19 and 20

3rd OUTPUT: Lets enter the values a = 45, b = 36, c= 96

Please Enter three different values
45
36
96

96 is Greater Than both 45 and 36

Lets enter a = 5, b = 5, c= 5

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 greatest of three numbers in c program helps the user to enter three different values. Next, this C program to Find Largest of Three numbers using Nested If in C .

/* C Program to find Largest of Three numbers using Nested 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>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 largest of three numbers example

  1. First, if condition check whether a-b is greater than 0 and a-c is greater than 0. If we subtract a small number from a big number then this condition fail, 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 greater than 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: Lets enter the values a = 56, b = 222, c= 98

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, C Program find the largest among three numbers using Conditional Operator

/* C Program to find Largest of 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 number among three is: %d", largest);
 return 0;
}

Within this C program to find the greatest of three numbers 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 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 for this C Program to Find Largest of Three numbers

Please Enter three different values
56
9
26

Largest number among three is: 56

a = 12, b = 48, c= 39

Please Enter three different values
12
48
39

Largest number among three is: 48

a = 2, b = 56, c= 98

Please Enter three different values
2
56
98

Largest number among three is: 98

Comments are closed.