Greatest among three numbers in C - 👑 सार्थक मुंड S3 🤴🏻

Greatest among three numbers in C

greatest among three numbers in c

Write a program (WAP) in C to read three numbers from keyboard(User Input) and find-out the greatest among three numbers.

1. Using Simple if :-

#include <stdio.h>
int main()
{
  float n1, n2, n3;
  printf("Enter three numbers :\n ");
  scanf("%d%d%d",n1,n2,n3);
  if(n1 >= n2 && n1 >= n3)
  {
    printf("Largest number is : ",n1);
  }
  if(n2 >= n1 && n2 >= n3)
  {
    printf("Largest number is : ",n2);
  }
  if(n3 >= n1 && n3 >= n2)
  {
    printf("Largest number is : ",n3);
  }
    return 0;
  }

Try it Yourself ➠
greatest among three numbers in c

2. Using if...else :-

#include <stdio.h>
int main()
{
  float n1, n2, n3;
  printf("Enter three numbers :\n ");
  scanf("%d%d%d",n1,n2,n3);
  if((n1 >= n2) && (n1 >= n3))
    printf("Largest number is : ",n1);
  else if ((n2 >= n1) && (n2 >= n3))
    printf("Largest number is : ",n2);
  else
    printf("Largest number is : ",n3);
return 0;
}

Try it Yourself ➠
greatest among three numbers in c

3. Using Nested if...else :-

#include <stdio.h>
int main()
{
  float n1, n2, n3;
  printf("Enter three numbers :\n ");
  scanf("%d%d%d",n1,n2,n3);
  if (n1 >= n2)
  {
    if (n1 >= n3)
     printf("Largest number is : ",n1);
    else
     printf("Largest number is : ",n3);
  }
  else
  {
    if (n2 >= n3)
      printf("Largest number is : ",n2);
    else
      printf("Largest number is : ",n3);
   }
return 0;
}

Try it Yourself ➠
greatest among three numbers in c

4. Using Ternary Operator :-

#include<stdio.h>
int main()
 {
 int n1,n2,n3,large;
 printf("Enter any three numbers:\n");
 scanf("%d%d%d",n1,n2,n3);
 large=(n1>n2)?(n1>n3?n1:n3):(n2>n3?n2:n3);
 printf("Largest number: ",large);
 return 0;
}

Try it Yourself ➠
greatest among three numbers in c
Must Read :-
  1. Blogger Tutorial
  2. C Programs
  3. C++ Programs
  4. Java Programs
  5. Python Programs
  6. Programming Notes 
  7. Questions & Answers
  8. My Codes : HTML, CSS & JS
  9. All Subject Notes
Previous article
Next article

Leave Comments

Post a Comment

Articles Ads

Articles Ads 1

Articles Ads 2

Advertisement Ads