Answers ( 2 )

  1. 21 Mar 2025

    Here is the simple program in c to find the greatest among three number :

    #include <stdio.h>
    
    int main() {
        int num1, num2, num3;
    
        printf("Enter the first number: ");
        scanf("%d", &num1);
    
        printf("Enter the second number: ");
        scanf("%d", &num2);
    
        printf("Enter the third number: ");
        scanf("%d", &num3);
    
        if (num1 >= num2 && num1 >= num3) {
            printf("The greatest number is: %d\n", num1);
        } else if (num2 >= num1 && num2 >= num3) {
            printf("The greatest number is: %d\n", num2);
        } else {
            printf("The greatest number is: %d\n", num3);
        }
    
        return 0;
    }
    
    
    
    

    2
  2. 22 Mar 2025

    You make it short using loop, get user input in array like this

    #include <stdio.h>
     int main() {
        int num[3];
    
        for (int i = 0; i < 3; i++) {
            printf("Enter number %d: ", i + 1);
            scanf("%d", &num[i]);
        }
    
        if (num[0] >= num[1] && num[0] >= num[2]) {
    
    
            printf("The greatest number is First number: %d\n", num[0]);
    
    
        } else if (num[1] >= num[0] && num[1] >= num[2]) {
    
    
            printf("The greatest number is Second number: %d\n", num[1]);
    
    
        } else {
            printf("The greatest number is third number: %d\n", num[2]);
        }
        return 0;
    }    
    
    

    also you can use ternary operator 

    int max = (num[0] > num[1]) ? ((num[0] > num[2]) ? num[0] : num[2]) : ((num[1] > num[2]) ? num[1] : num[2]); 
    // now print max
    printf("The greatest number is: %d\n", max); 
    

    0

Leave an Answer


umesh

Ask by umesh

 Prev question

Next question