Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

is this correct usage of nested if

+15 votes
asked Aug 4, 2023 by misab (220 points)
int a,b,c;
   printf("enter 3 number");
   scanf("%d%d%d",&a,&b,&c);
   if (a<b){
       
        if (b<c){
            printf("the gretest number is %d ", c);
        
            
       }else {
           printf("the gretest number is %d ", b);
           
       }
       
           
      
   }else {
       printf("the gretest number is %d ",a);
   }

    return 0;
}

3 Answers

+3 votes
answered Aug 5, 2023 by Peter Minarik (86,240 points)
edited Aug 6, 2023 by Peter Minarik

Assuming your code is correctly put into a function, then yes, this is a syntactically correct nested if, the code compiles and runs without any errors.

However, the logic you implemented to find the largest number of 3 numbers is not correct.

If you enter the input "2 1 3" your program says, the largest number is 2 (but we all know 3 is the largest).

Try to analyze your code, what would it do with the above input and you'll figure out how to fix it.

If you need further help, ask away!

Good luck! :)

0 votes
answered Aug 7, 2023 by PAWAN RAJ (150 points)

This code not completely correct because 'a' has been not compared with 'c'.

The correct can be written this type

 int a,b,c;
   printf("enter 3 number");
   scanf("%d%d%d",&a,&b,&c);
   if (a<b){
       
        if (b<c){
            printf("the gretest number is %d ", c);
        
            
       }else {
           printf("the gretest number is %d ", b);
           
       }
       
           
      
   }else {

if(a>c){
       printf("the gretest number is %d ",a);

else{

printf("the gretest number is %d ",c);
   }

    return 0;
}

commented Aug 21, 2023 by winapiadmin (210 points)
You have a lot of syntax errors in this code.
This is the fixed code:

   int a,b,c;
   printf("enter 3 number: ");
   scanf("%d%d%d",&a,&b,&c);
   if (a<b){
        if (b<c){
        printf("the gretest number is %d ", c);
        } else {
        printf("the gretest number is %d ", b);
    } else {
        if(a>c){
            printf("the gretest number is %d ",a);
        } else {
            printf("the gretest number is %d ",c);
        }
    }
    return 0;
0 votes
answered Aug 25, 2023 by yash bhatt (220 points)
In nested if statements we have to use the if followed by elseif then another elseif as many as require and lastly use else here is your corrected code.

#include <stdio.h>

int main() {
    int a, b, c;
    printf("Enter 3 numbers: ");
    scanf("%d%d%d", &a, &b, &c);

    if (a == b && b == c) {
        printf("All three numbers are equal: %d\n", a);
    } else if (a >= b && a >= c) {
        printf("The greatest number is %d\n", a);
    } else if (b >= a && b >= c) {
        printf("The greatest number is %d\n", b);
    } else {
        printf("The greatest number is %d\n", c);
    }

    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...