0
char choice, y, n; float percentage; int marks; printf("Do you want to know your percentage?(y/n):\n\n"); scanf(" %c", &choice); if (choice == 'y') { printf("Enter Your 2nd Sem Marks:\n\n"); scanf(" %d", &marks); percentage = (marks / 775) * 100; printf("Your 2nd Sem Percentage is %.2f", percentage); } else printf("Thankyou!!!!\n\n\n"); 

I can't calculate the percentage using the above code - I get output 0.00...

For example when i give input as 536,instead of showing result as 69.16,it shows 0.00 please help me out

2
  • possible duplicate of What is the behavior of integer division in C? Commented Aug 30, 2014 at 15:37
  • 1
    You've indicated through comments that two of the answers to your question have worked. There's one more thing that you should do, and that is to mark one of the answers as "accepted". This is one of the ways the person answering your question benefits from the time taken to answer. Commented Aug 30, 2014 at 16:01

2 Answers 2

5

Since marks is an int and will always be less than 775, the expression (marks / 775), after integer division, will always yield zero. You should cast to a floating point number in the expression, or otherwise have a floating point number in the expression, to preserve the remainder of the division.

Sign up to request clarification or add additional context in comments.

Comments

3

This is because you are doing this

percentage = (marks/775) * 100; 

You should instead do this

percentage = ((float)marks/775) * 100; 

Remember that (marks/775) will give an integer as both the operands are integers. Int his case, 536/775 will be 0 as per integer division.

You should instead be performing float division for getting the percentage.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.