# A problem with percentage variable

• 01-13-2013
shansajid
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
• 01-13-2013
Salem
> percent=marks/1100*100;
You need to make the right hand side into a float expression before evaluating it (think of a cast).
• 01-13-2013
std10093
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.
• 01-13-2013
c99tutorial
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
• 01-14-2013
shansajid
Thanks for help. It runs.....
• 01-14-2013
Derek Lake
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.
• 01-14-2013
std10093
NEVER USE system("pause") and similarly things

//you are welcome :)
• 01-14-2013
iMalc
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.