Sabina Yasmin
Sabina Yasmin

Reputation: 47

Why my C program is not working for Grading points?

I have written a C program to find 5 students grade point. I shows correct result if a student failed but when all student pass it is not showing any answer. Please help me to solve it. Code:

#include <stdio.h>
int main()
{
    int mark[5], failed = 0, sum = 0;
    for (int i = 0; i < 5; i++) scanf("%d", &mark[i]);
    for (int i = 0; i < 5; i++)
    {
        sum = sum + mark[i];
        if (mark[i] < 40) failed++;
    }
    double marks = (double)((sum / 500) * 100);
    if (failed > 0) printf("Ops! Failed\n");
    else
    {
        if (marks >= 90) printf("%d%%: Grade A", marks);
        else if (marks >= 80 && marks <= 89) printf("%.2lf%%: Grade B\n", marks);
        else if (marks >= 70 && marks <= 79) printf("%.2lf%%: Grade C\n", marks);
        else if (marks >= 60 && marks <= 69) printf("%.2lf%%: Grade C\n", marks);
        else if (marks >= 40 && marks <= 59) printf("%.2lf%%: Grade D\n", marks);
    }
    return 0;
}

Wrong output image: enter image description here

Upvotes: 0

Views: 98

Answers (2)

Your program has two main syntactical issues:

1.

double marks = (double)((sum / 500) * 100);

(sum / 500) uses integer arithmetic. The fraction part is truncated towards zero. The explicit cast to double just converts the result value of the whole arithmetic expression to a double.

Either use

double marks = ((double) sum / 500) * 100);

or

just specify the variable sum of type double. With that you can also omit the cast to double at the initialization of marks.

double sum;
....

double marks = ((sum / 500) * 100);

2.

if (marks >= 90) printf("%d%%: Grade A", marks);

You use the %d conversion specifier to print a double value of marks, which invokes undefined behavior.

Use %f, or, if you want to stick to convention/consensus, %.2lf like you did at all other printf()s.

Note: For printf() you can use the %f conversion specifier to print a double value, not necessary %lf like it is required at scanf() to consume a double.


Side note:

  • To be standard-compliant, change int main() to int main(void).

Upvotes: 2

pifor
pifor

Reputation: 7892

In the following code I have also changed the upper bounds (otherwise you don't display anything if marks=79.8 for example):

#include <stdio.h>
int main()
{
    int mark[5], failed = 0, sum = 0;
    for (int i = 0; i < 5; i++) scanf("%d", &mark[i]);
    for (int i = 0; i < 5; i++)
    {
        sum = sum + mark[i];
        if (mark[i] < 40) failed++;
    }
    double marks = (((double)sum / 500) * 100);
    printf("marks=%f\n", marks);
    if (failed > 0) printf("Ops! Failed\n");
    else
    {
        if (marks >= 90) printf("%.2f%%: Grade A\n", marks);
        else if (marks >= 80 && marks < 90) printf("%.2f%% Grade B\n", marks);
        else if (marks >= 70 && marks < 80) printf("%.2f%% Grade C\n", marks);
        else if (marks >= 60 && marks < 70) printf("%.2f%% Grade C\n", marks);
        else if (marks >= 40 && marks < 60) printf("%.2f%% Grade D\n", marks);
    }
    return 0;
}

Upvotes: 1

Related Questions