i m new in c language. i write the following code. its not giving the correct input. when i enter the marks it always give ouput "F" .please correct the code

#include <stdio.h>

void main()
	int marks;
	float percentage;
     printf("enter marks:");

	if (percentage>80)

		printf(" A+ ");

 if(percentage >=70 && percentage<80)
    	printf(" A");

	 if(percentage >=60 && percentage<70)
    	printf(" B");

	if(percentage >=50 && percentage<60)
    	printf(" C");

	 if(percentage >=40 && percentage<50)
    	printf(" D");
if(percentage >=33 && percentage<40)
    	printf(" E");

		printf(" F");


Edited 6 Years Ago by WaltP: Added CODE Tags

well, the main problem is, your math doesn't make any sense, at least not to any conventional system of grading percentages. but let's try and fix this as best we can...


besides begging the obvious question "why are you doing this?" .... even if this is really what you want to do, it won't work right. Let's start with the part that doesn't work:

(marks/1100), both parts of this are integers. assuming your "marks" is a percentage value of roughly 100 or less, this equation will always evaluate as zero. How so? Okay, say marks=100. 100/1100 = 0. Because they're evaluated as "int". Division of integers always drops any fractional part, and only looks at the whole number. the rest of the equation is now meaningless, because 0 * 100 is still zero. your "percentage" of 0 is converted (correctly) as "F"

when you divide integers in an equation that needs to be evaluated as a float, you need to cast the integer as a float. so, to be evaluated properly, your equation would look like this:


Now it "works", but it still doesnt make sense. Using the same example, marks=100, this now evaluates as (float)marks/1100 = 0.0909 . you can muliply this by the rest of the equation, so that you get a non-zero answer. 0.0909 * 100 = 9.0909. well, guess what, 9.09 is still an "F" ... that's why im saying your entire equation doesn't make sense.

Edited 6 Years Ago by jephthah: n/a

This article has been dead for over six months. Start a new discussion instead.