I'm studying language c, but it's not working well. The score is an A to F grade, and it's an inequality question.

Asked 2 years ago, Updated 2 years ago, 28 views

#include<stdio.h>
int main()
{
    int score;
    scanf("%d",&score);
    if(100>=score>89)
        printf("A");
    else if(89>=score>=80)
        printf("B");
    else if(79>=score>=70)
        printf("C");
    else if(69>=score>=60)
        printf("D");
    else if(60>score)
        printf("F");
}

And

#include<stdio.h>
int main()
{
    int score;
    scanf("%d",&score);
    if(90<=score<=100)
        printf("A");
    else if(80<=score<=89)
        printf("B");
    else if(70<=score<=79)
        printf("C");
    else if(60<=score<=69)
        printf("D");
    else if(score<60)
        printf("F");
}

The difference is only an inequality sign, so why can't it be implemented? Do I have to do small number <x<large number?

c c++

2022-09-20 20:48

1 Answers

In C language, if (100>=score>89) should not be written like this, but if (100>=score&&score>89).

&&& is a logic-and operator, and the whole is true when the left one is true and the right one is true.

There's no one who's good from the beginning. Keep up the good work and good luck.

#include<stdio.h>
int main()
{
    int score;
    scanf("%d",&score);
    if(100>=score && score>89)
        printf("A");
    else if(89>=score && score>=80)
        printf("B");
    else if(79>=score && score>=70)
        printf("C");
    else if(69>=score && score>=60)
        printf("D");
    else if(60>score)
        printf("F");
}


2022-09-20 20:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.