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

NetVipeC
  • 4,402
  • 1
  • 17
  • 19
tajammul pasha
  • 11
  • 1
  • 1
  • 2
  • possible duplicate of [What is the behavior of integer division in C?](http://stackoverflow.com/questions/3602827/what-is-the-behavior-of-integer-division-in-c) – juanchopanza Aug 30 '14 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. – DavidO Aug 30 '14 at 16:01

2 Answers2

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.

jackarms
  • 1,343
  • 7
  • 14
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.

OneMoreError
  • 7,518
  • 20
  • 73
  • 112