Greatest of three numbers-C programming


 C program to find greatest of three integers

To find greatest among three integers we will use if-else conditional statement. Here, in this post we will learn how to find greatest of three integers in C programming using if-else conditionals statements. If the number is greater than the other two numbers then that number will be the greatest number among three.

Algorithm:-

  1. Start
  2. Declare 3 variable a, b, c.
  3. Take input from the user.
  4. Check which number is greater using if else statements
  5. print the largest value on the screen.
  6. Stop.
First we check if a is greater than b and c if yes then we will print it is the greater element than b and c, if its not then we check if the element is greater than both the a and c, if yes then print b as greater and if both the conditions fails then obviously c is the largest element so print it. That complete our program refer the code given below.

Program:-


#include <stdio.h>

int main()
{
    
    int a-12, b=23, c=8;
    
    
    if((a > b) && (a > c)){
        printf("%d is greater than %d and %d", a, b, c);
    }
    else if((b > a) && (b > c)){
        printf("%d is greater than %d and %d", b, a, c);
    }else{
        printf("%d is greater than %d and %d", c, a, b);
    }

    return 0;
}

Output:-

Enter any 3 numbers: 12 23 8                                                                                          
23 is greater than 12 and 8


Program with user input:-


#include <stdio.h>

int main()
{
    
    int a, b, c;
    
    printf("Enter any 3 numbers: ");
    scanf("%d %d %d", &a, &b, &c);
    
    if((a > b) && (a > c)){
        printf("%d is greater than %d and %d", a, b, c);
    }
    else if((b > a) && (b > c)){
        printf("%d is greater than %d and %d", b, a, c);
    }else{
        printf("%d is greater than %d and %d", c, a, b);
    }

    return 0;
}

Output:-

Enter any 3 numbers: 12 23 8                                                                                          
23 is greater than 12 and 8

Enter any 3 numbers: 127 45 90                                                                                        
127 is greater than 45 and 90 


Program:-

C program to find greatest of 3 numbers using nested if-else statements in c programming.

In this program, we find greatest of 3 integers using nested if-else statements. If a is greater than b, then check again if a greater than c if yes, then print a is greater than b and c. But if a is less than b then checks if b is greater than c if yes, then print b is greater than a and c else print c is greater than a and b.

#include <stdio.h>


int main()
{
    
    int a, b, c;
    
    printf("Enter any 3 numbers: ");
    scanf("%d %d %d", &a, &b, &c);
    
    if(a>b) {
        if(a>c) {
            printf("%d is greater than %d and %d", a, b, c);
        } else {
            printf("%d is greater than %d and %d", c, b, a);
        }
    } else {
        if(b>c) {
            printf("%d is greater than %d and %d", b, a, c);
        } else {
            printf("%d is greater than %d and %d", c, a, b);
        }
    }

    return 0;
}

Output:-

Enter any 3 numbers: 12 23 8                                                                                          
23 is greater than 12 and 8

Post a Comment

0 Comments