C Program To Find Largest And Smallest of Three Numbers (3 Different Ways)

In this article, we are going to write a c program to find the largest and smallest number among Three Numbers.

We will make this program in the following way -:

  • C Program To Find Largest And Smallest of Three Numbers Using if Else
  • C Program To Find Largest And Smallest of Three Numbers Using Function
  • C Program To Find Largest And Smallest of Three Numbers Using Conditional or Ternary Operator

To make this program, we will use the following concept given below. If you do not know about these topics well, then you may not understand this program, so you should know about them in detail from the link given below.

Now without wasting time let’s start programming.

C Program To Find Largest And Smallest of Three Numbers

C Program To Find Largest And Smallest of Three Numbers Using if else

Algorithm -:

  1. Program Start
  2. Variable Declaration
  3. Input three number
  4. Check the condition
  5. Display Largest Number
  6. Display Smallest Number
  7. Program End

Program -:

//C program to find Largest and smallest among three numbers Using if else

#include<stdio.h>
void main()
{
  // Variable declaration
   int a,b,c, larg, small;

   printf("Enter Three Number\n");
   scanf("%d %d %d",&a,&b,&c);

  // larg among a, b and c
  if(a>b)
  {
      if(a>c)
        larg = a;
      else
        larg = c;
  }
  else
  {
      if(b>c)
        larg = b;
      else
        larg = c;
  }
 // Small among a, b and c
  if(a<b)
  {
      if(a<c)
        small = a;
      else
        small = c;
  }
  else
  {
      if(b<c)
        small = b;
      else
        small = c;
  }
//Display Largest Number
 printf("%d is largest number\n",larg);

//Display smallest number
 printf("%d is Smallest Number ",small);

return 0;
}

Output -:

Enter Three Numbers
119
372
721

721 is largest number
119 is Smallest Number

C Program To Find Largest And Smallest of Three Numbers Using function

Program -:

//C program to find the Largest and smallest of three numbers Using Function

#include<stdio.h>
void larg(int, int, int);
void small(int, int, int);
int main()
{
  // Variable declaration
   int a,b,c;

   printf("Enter Three Numbers\n");
   scanf("%d %d %d",&a,&b,&c);

   //calling function to find Largest number
    larg(a,b,c);
    small(a,b,c);

   return 0;
}
void larg(int a, int b, int c)
{  int larg;
     // Larg among a, b and c
  if(a>b)
  {
      if(a>c)
        larg = a;
      else
        larg = c;
  }
  else
  {
      if(b>c)
        larg = b;
      else
        larg = c;
  }
//Display Largest number
    printf("Largest Number is : %d",larg);
}
void small(int a, int b, int c)
{  int small;
     // Small among a, b and c
  if(a<b)
  {
      if(a<c)
        small = a;
      else
        small = c;
  }
  else
  {
      if(b<c)
        small = b;
      else
        small = c;
  }

  //Display smallest number
    printf("\nSmallest Number is : %d",small);

}

Output -:

Enter Three Numbers
20
34
22

Largest number is 34
Smallest Number is 20

C Program To Find Largest And Smallest of Three Numbers Using Conditional Operator

Program -:

//C program to find largest and smallest among three numbers using ternary operator

#include<stdio.h>

void main()
{
  // Variable declaration
   int a,b,c,larg,small;

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

 // Largest among a, b and c
   larg = a>b?a>c?a:c:b>c?b:c;

   // Small among a, b and c
   small = a<b?a<c?a:c:b<c?b:c;

 //Display largest number
   printf("Largest Number is : %d\n",larg);

//Display smallest number
   printf("Smallest Among 3 Number is : %d",small);

}

Output -:

Enter Three Numbers
2
4
1

Largest number is 4
Smallest Number is 1

Conclusion

In this article, we have created all the following programs -:

  • C Program To Find Largest And Smallest of Three Numbers Using if Else
  • C Program To Find Largest And Smallest of Three Numbers Using Function
  • C Program To Find Largest And Smallest of Three Numbers Using Conditional or Ternary Operator

If you have difficulty to understand any program or have any doubts or questions, then tell me in the comment below.

Jeetu Sahu is A Web Developer | Computer Engineer | Passionate about Coding, Competitive Programming, and Blogging

Leave a Comment