# Thread: A problem with percentage variable

1. ## A problem with percentage variable

Why my output is still 0?
Code:
#include <stdio.h>
#include <conio.h>
void main()
{
int marks;
float percent;

printf("\nEnter your marks (out of 1100): ");
scanf("%d", &marks);
percent=marks/1100*100;
printf("\n\n%f", percent);

getch();
}
OUTPUT:

0

2. > percent=marks/1100*100;
You need to make the right hand side into a float expression before evaluating it (think of a cast).

3. Moreover, take a look at Salem's avatar.

You should write
Code:
int main(void)
and not
Code:
void main()
Also, it would be good to have a return 0; before main ends, like this
Code:
getch();
return 0;
}
Then mind that Conio.h is non-standard, and thus I suggest you never use it.

4. percent=marks/1100*100;

In C there is integer math and floating-point math and each operation happens between their respective types. If marks is an integer, say, 1099, then the first part does this

1099 / 1100

which gives 0 as a result because 0.99 is not an integer. On to the next part:

0 * 100

which gives 0 as a result.

The safest way is to specify explicitly or implicitly that all members are type float.

percent = (float)marks / 1100.0f * 100.0f

5. Thanks for help. It runs.....

6. Hey shansajid.

A couple more tips:
Instead of using getch(), you can make a garbage variable and scan it in. This will put a prompt to the user so the program doesn't automatically exit.

Another thing you can note is that if you are setting a float equal to some computation involving only integers, and you are performing division, you can always add ".0" to the end.
For instance you have percent = marks/1100
You can write percent = marks/1100.0 instead, and the float will store the decimal division instead of the integer division.

EDIT: Before, I had in this post "Alternatively, you can use system("Pause"); at the end. Make sure you use #include <stdio.h>"
This is a command in Windows only. If you are programming in a UNIX based system, this will not work. Also, it's not very efficient. It does pause the command line, though.
Thanks for the correction post, std10093.

7. NEVER USE system("pause") and similarly things

//you are welcome

8. You're better off doing the multiply first and so doing it with all integer math:
Code:
percent = 100 * marks / 1100;
But hey it's not really going to make any important difference here.