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.

something wrong in my program, please help me. how to prevent other statements from following, if score>77

+1 vote
asked Oct 6, 2021 by Ahmad Muamar Rijal (130 points)
#include <stdio.h>

int main()

    {

    float uts,uas,assignment,attendance,score;

    printf("enter student grades\n");

    printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");

    printf("Enter UTS grades: ");

    scanf("%f",&uts);

    printf(Enter UAS grades": ");

    scanf("%f",&uas);

    printf("enter assignment value: ");

    scanf("%f",& assignment);

    printf("Enter attendance: ");

    scanf("%f",&attendance);

    

    score=(0.3*uts)+(0.35*uas)+(0.3*assignment) + (0.05*(attendance/14));

    

    if (score>77) {printf("YOUR LETTERS VALUES ARE: A\n");

    }if (score>70) {printf("YOUR LETTERS VALUES ARE: AB\n");

    }if (score>63) {printf("YOUR LETTERS VALUES ARE: B\n");

    }if (score>55) {printf("YOUR LETTERS VALUES ARE: BC\n");

    }if (score>45) {printf("YOUR LETTERS VALUES ARE: C\n");

    }if (score>25) {printf("YOUR LETTERS VALUES ARE: D\n");

    }if (score<25) {printf("YOUR LETTERS VALUES ARE: E\n");

    }

return 0;

}

RESULT:

enter student grades

Enter UTS grades: 90

Enter UAS grades: 90

Enter assignment grades: 90

enter attendance: 14

YOUR LETTERS VALUES ARE: A

YOUR LETTERS VALUES ARE: AB

YOUR LETTERS VALUES ARE: B

YOUR LETTERS VALUES ARE: BC

YOUR LETTERS VALUES ARE: C

YOUR LETTERS VALUES ARE: D

the statement i mean is the "YOUR LATTERS VALUES IS"

2 Answers

+1 vote
answered Oct 7, 2021 by Peter Minarik (86,040 points)
edited Oct 7, 2021 by Peter Minarik

The syntax of if is as below (with the else [statement] being optional)

if ([condition])
    [statement]
else
    [statement]

Now using this, your could should look like this:

#include <stdio.h>

int main()
{
    float uts, uas, assignment, attendance;
    printf("Enter student grades\n");
    printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
    printf("Enter UTS grades: ");
    scanf("%f", &uts);
    printf("Enter UAS grades: ");
    scanf("%f", &uas);
    printf("Enter assignment value: ");
    scanf("%f", &assignment);
    printf("Enter attendance: ");
    scanf("%f", &attendance);

    float score = (0.3f * uts) + (0.35f * uas) + (0.3f * assignment) + (0.05f * (attendance / 14));

    if (score > 77.0f)
    {
        printf("YOUR LETTERS VALUES ARE: A\n");
    }
    else if (score > 70.0f)
    {
        printf("YOUR LETTERS VALUES ARE: AB\n");
    }
    else if (score > 63.0f)
    {
        printf("YOUR LETTERS VALUES ARE: B\n");
    }
    else if (score > 55.0f)
    {
        printf("YOUR LETTERS VALUES ARE: BC\n");
    }
    else if (score > 45.0f)
    {
        printf("YOUR LETTERS VALUES ARE: C\n");
    }
    else if (score > 25.0f)
    {
        printf("YOUR LETTERS VALUES ARE: D\n");
    }
    else
    {
        printf("YOUR LETTERS VALUES ARE: E\n");
    }
    
    return 0;
}

Notes:

  1. I applied a bit of formatting
  2. I added the f suffix after the number literals to distinguish a float from a double type.
  3. Your original code was wrong because it didn't handle score == 25 case. The above code handles that case correctly.
  4. printf(Enter UAS grades": "); line had the quotes in the wrong locations. I fixed it in the code.
commented Oct 12, 2021 by Ahmad Muamar Rijal (130 points)
thanks bro for helping to solve my problem.
0 votes
answered Oct 7, 2021 by Play with ROMS (140 points)
#include <stdio.h>

int main()

{

    float uts,uas,assignment,attendance,score;

    printf("enter student grades\n");

    printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");

    printf("Enter UTS grades: ");

    scanf("%f",&uts);

    printf("Enter UAS grades: ");

    scanf("%f",&uas);

    printf("enter assignment value: ");

    scanf("%f",&assignment);

    printf("Enter attendance: ");

    scanf("%f",&attendance);

    

    score=(0.3*uts)+(0.35*uas)+(0.3*assignment) + (0.05*(attendance/14));

    

    if (score>77)
    {
        printf("YOUR LETTERS VALUES ARE: A\n");

    }
    else if (score>70)
    {
        printf("YOUR LETTERS VALUES ARE: AB\n");

    }
    else if (score>63)
    {
        printf("YOUR LETTERS VALUES ARE: B\n");

    }
    else if (score>55)
    {
        printf("YOUR LETTERS VALUES ARE: BC\n");

    }
    else if (score>45)
    {
        printf("YOUR LETTERS VALUES ARE: C\n");

    }
    else if (score>25)
    {
        printf("YOUR LETTERS VALUES ARE: D\n");

    }
    else if (score<25)
    {
        printf("YOUR LETTERS VALUES ARE: E\n");

    }

return 0;

}

RESULT:

enter student grades
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter UTS grades: 99
Enter UAS grades: 99
enter assignment value: 99
Enter attendance: 99
YOUR LETTERS VALUES ARE: A

?? just use else if if you have multiple options or switch case. it will be much more easier
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.
...