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.

Why is there 2 "0"s on my output in "Tuition Fee Discount"?

+1 vote
asked Nov 18, 2021 by Lexus Guevara (1,010 points)
int main()
{
    float Grades;
    char Average;
    int Tuition;
    
//Formula for the Grades
    Grades = ( 90.00 + 87.00 + 99.00 + 75.00 + 77.00 + 80.00 + 100.00 + 94.00 + 83.00 ) / 9;
    
    
//Output    
    printf("Intro to Computing                :     90\n");
    printf("Fundamentals of Programming       :     87\n");
    printf("Business Application Software     :     99\n");
    printf("Understanding the Self            :     75\n");
    printf("Purposive Communication           :     77\n");
    printf("Mathematics in the Modern World   :     80\n");
    printf("Physical Fitness and Wellness     :     100\n");
    printf("Reading in the Philippine History :     94\n");
    printf("NSTP1                             :     83\n");
    printf("---------------------------------------------\n");
    printf("General Weighted Average          :     %.2f\n", Grades);
    
//Scholarship Grant       
    printf("Scholarship Grant                 :     %c", Average);
    if (Grades <= 100 && Grades >= 98) {
        printf("President's List");
    }
    else if (Grades <= 97 && Grades >= 95) {
        printf("College Scholar");
    }
    else if (Grades <= 94 && Grades >= 92) {
        printf("Dean's List");
    }
    else {
        printf("None");
    }
    
//Tuition Fee Discount  
    printf("\nTuition Fee Discount              :     %d", Tuition);
    if (Grades <= 100 && Grades >= 98) {
        printf("100%%");
    }
    else if (Grades <= 97 && Grades >= 95) {
        printf("50%%");
    }
    else if (Grades <= 94 && Grades >= 92) {
        printf("30%%");
    }
    else {
        printf("0%%");
    }
    return 0;
}

1 Answer

0 votes
answered Nov 18, 2021 by Peter Minarik (86,130 points)

To have 0% printed on the output, your code must have ended up here:

    else {
        printf("0%%");
    }

You can easily verify this by printing out the value of Grades:

printf("Grades: %f\n", Grades);

You can see the value of Grades is 87.222221, hence your code does what it does.

Also, I think your code doesn't really work the way you'd like it to.

    if (Grades <= 100 && Grades >= 98) {
        printf("100%%");
    }
    else if (Grades <= 97 && Grades >= 95) {
        printf("50%%");
    }
    else if (Grades <= 94 && Grades >= 92) {
        printf("30%%");
    }
    else {
        printf("0%%");
    }

What happens for instance if Grades = 97.7?  Well, it ends up printing 0.

I have a feeling what you really want would be better described by this piece of code:

    float discount = 0.0f;
    if (Grades <= 100.0f)
    {
        if (Grades >= 98.0f)
            discount = 100.0f;
        else if (Grades >= 95.0f)
            discount = 50.0f;
        else if (Grades >= 92.0f)
            discount = 30.0f;
    }
    printf("%.2f%%", discount);
    

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.
...