Home > Back-end >  c program if else if statement
c program if else if statement

Time:09-25

facing problem in second and third else if statement. when i run the code it shows the printf statements of first if and first else if statement except for its own printf statements.how to solve it please help anyone

#include <stdio.h>

int main()

{
int sci_score, maths_score;

printf("enter your sci score:", sci_score);

scanf("%d", &sci_score);

printf("enter your maths score:", maths_score);

scanf("%d", &maths_score);

if(sci_score>=33, maths_score>=33) 
{
    printf("you are awarded with ruppees 45");
}

else if(sci_score>=33, maths_score<33)
{
    printf("you are awarded with ruppees 15. work hard in maths");
}

else if(sci_score<33, maths_score>=33)
{
    printf("you are awarded with ruppees 15. work hard in sci");
}

else if(sci_score<33, maths_score<33) 
{
    printf("work hard in sci and maths");
}

return 0;

}

CodePudding user response:

Use && operator to match the conditions

int main()

{
int sci_score, maths_score;

printf("enter your sci score:", sci_score);

scanf("%d", &sci_score);

printf("enter your maths score:", maths_score);

scanf("%d", &maths_score);

if(sci_score>=33 && maths_score>=33) 
{
    printf("you are awarded with ruppees 45");
}

else if(sci_score>=33 && maths_score<33)
{
    printf("you are awarded with ruppees 15. work hard in maths");
}

else if(sci_score<33 && maths_score>=33)
{
    printf("you are awarded with ruppees 15. work hard in sci");
}

else if(sci_score<33 && maths_score<33) 
{
    printf("work hard in sci and maths");
}

return 0;
}

CodePudding user response:

All you need to do is remove the comma , in your if/else if statement and replace with && this means both the conditions in the if/else if block needs to be true for this specific condition to display the print statement,

Updated Code:

#include <stdio.h>

int main()

{
int sci_score, maths_score;

printf("enter your sci score:", sci_score);

scanf("%d", &sci_score);

printf("enter your maths score:", maths_score);

scanf("%d", &maths_score);

if(sci_score>=33 && maths_score>=33) 
{
    printf("you are awarded with ruppees 45");
}

else if(sci_score>=33 && maths_score<33)
{
    printf("you are awarded with ruppees 15. work hard in maths");
}

else if(sci_score<32 && maths_score>=33)
{
    printf("you are awarded with ruppees 15. work hard in sci");
}

else if(sci_score<33 && maths_score<33) 
{
    printf("work hard in sci and maths");
}
return 0;
}

CodePudding user response:

I'm borrowing your words here, you need to word hard on understanding C operators first. What you are trying to do for each if || else if statement is to satisfy both score conditions, which require logical operators.

But you are using comma operator, which is very rarely used in C. I actually never use it and have to look it up after reading your question. Because I could not understand at first why your program still compiled. Guess I also need to work on this too.

  • Related