Q:

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

belongs to collection: Basic C Programming Examples

0

you have to 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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Basic C Programming Examples

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C Program To Find Largest And Smallest Number Amon... >>
<< C Program To Find Largest of Three Numbers ( 3 Dif...