C Program To Find Greatest of Three Numbers

हेलो फ्रेंड्स आज के इस उदाहरण में, हम यूजर द्वारा इंटर किए गए तीन नंबरों में से सबसे बड़ी संख्या का पता लगाना सीखेंगे।

C Program To Find Greatest of Three Numbers
C Program To Find Greatest of Three Numbers

Program to Find Greatest of Three Numbers

इस प्रोग्राम में हम यूजर से तीन नंबर्स इनपुट के रूप में लेंगे फिर उन नंबर्स के बिच करके ये बनाएंगे की कौन सा नंबर बड़ा है | इस प्रोग्राम को हम कई अलग अलग तरीको से बनाएंगे |

5 Different Way To Find Greatest of Three Numbers

  1. Using if Statement
  2. Using if…else Ladder
  3. Using Nested if…else
  4. Using Functions
  5. Using Ternary operator

आइये अब हम इन सभी तरीको से Largest Number चेक करना वाला प्रोग्राम बनाते है |

1. Write A Program to Find Greatest of Three Numbers in C Using if Statement

Algorithm-:

  1. Program Start
  2. Declaration of variable
  3. Enter the  number
  4. Assign the value in variable
  5. Check condition
  6. Give answer according to condition
  7. Program End

Program/Source Code

// C program to find the greatest among three numbers in C using if Statement

#include <stdio.h> 
int main() 
{ 
    int X, Y, Z;

 //Ask user to input any three integer numbers
    printf("Enter the numbers\n"); 

 //Store input values in variables for comparsion
    scanf("%d %d %d", &X, &Y, &Z); 

   // if X is greater than both Y and Z, X is the Greatest
    if (X >= Y && X>= Z) 
        printf("%d is the Greatest number.", X); 
  
// if Y is greater than both X and Z, Y is the Greatest
    if (Y >= X && Y >= Z) 
        printf("%d is the Greatest number.", Y); 
  
// if Z is greater than both X and Y, Z is the Greatest
    if (Z >= X && Z >= Y) 
        printf("%d is the Greatest number.", Z); 
  
    return 0; 
} 

Output -:

Enter the numbers
2
80
11
80 is the Greatest number.

2. Write A Program to Find Greatest of Three Numbers in C Using if else Ladder

// C program to find the greatest among three numbers in C using  if else Ladder

#include <stdio.h>
int main() 
{
    int num1, num2, num3;
    printf("Enter three numbers\n");
    scanf("%d %d %d", &num1, &num2, &num3);

    // if num1 is greater than both num2 and num3, num1 is the Greatest
    if (num1 >= num2 && num1 >= num3)
        printf("%d is the Greatest number.", num1);

    // if num2 is greater than both num1 and num3, num2 is the Greatest
    else if (num2 >= num1 && num2 >= num3)
        printf("%d is the Greatest number.", num2);

    // if both above conditions are false, num3 is the Greatest
    else
        printf("%d is the Greatest number.", num3);

    return 0;
}

Output -:

Enter three numbers
202
801
114
801 is the Greatest number.

3. Write A Program to Find Greatest of Three Numbers in C Using Nested if…else

// C program to find the greatest among three numbers in C using Nested if…else

#include <stdio.h> 
int main() 
{ 
    int X, Y, Z; 
  
    printf("Enter three numbers: "); 
    scanf("%d %d %d", &X, &Y, &Z); 
  
    if (X >= Y) { 
        if (X >= Z) 
            printf("%d is the Greatest number.", X); 
        else
            printf("%d is the Greatest number.", Z); 
    } 
    else { 
        if (Y >= Z) 
            printf("%d is the Greatest number.", Y); 
        else
            printf("%d is the Greatest number.", Z); 
    } 
  
    return 0; 
} 

Output -:

Enter three numbers
12
8
01
12 is the Greatest number.

4. Write A Program to Find Greatest of Three Numbers in C Using Functions

// C program to find the greatest among three numbers in C using functions
#include <stdio.h>>
int greatest_among_three(int a, int b, int c)
{
 if(a > b && a > c)
      return a;
 else if(b > a && b > c)
       return b;
  else
       return c;
}
int main()
{
  int x,y,z,max;
  printf("\nEnter three numbers : ");
  scanf("%d %d %d",&x,&y,&z);
  max = greatest_among_three(x,y,z);
  printf("\nThe Greatest among the three numbers is %d ", max);
  printf("\n");
  return 0;
}

Output -:

Enter three numbers :
43
66
21
The Greatest among the three numbers is 66

5. Write A Program to Find Greatest of Three Numbers in C Using Ternary operator or Conditional Operator

// C program to find the Greatest numbers in C using the conditional operator
#include <stdio.h>
int main()
{
  int x,y,z,max = 0;
  printf("\nEnter three numbers :");
  scanf("%d %d %d", &x,&y,&z);
 
  max = ((x > y) && (x > z)) ? x : ((y > x) && (y > z)) ? y : z;
  printf("\nThe Greatest among the three numbers is %d", max);

  return 0;
}

Output -:

Enter three numbers :
32
61
210
The Greatest among the three numbers is 210

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