Why my output is still 0?
OUTPUT: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(); }
0
This is a discussion on A problem with percentage variable within the C Programming forums, part of the General Programming Boards category; Why my output is still 0? Code: #include <stdio.h> #include <conio.h> void main() { int marks; float percent; printf("\nEnter your ...
Why my output is still 0?
OUTPUT: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(); }
0
> percent=marks/1100*100;
You need to make the right hand side into a float expression before evaluating it (think of a cast).
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I support http://www.ukip.org/ as the first necessary step to a free Europe.
Moreover, take a look at Salem's avatar.
You should write
and notCode:int main(void)
Also, it would be good to have a return 0; before main ends, like thisCode:void main()
Then mind that Conio.h is non-standard, and thus I suggest you never use it.Code:getch(); return 0; }
Code - functions and small libraries I use
__________________________________________________ __________________________________________________ ______________
It’s 2013 and I still use printf() for debugging.
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
Thanks for help. It runs.....
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.
Last edited by Derek Lake; 01-14-2013 at 09:49 AM.
NEVER USE system("pause") and similarly things
//you are welcome![]()
Last edited by std10093; 01-14-2013 at 09:52 AM.
Code - functions and small libraries I use
__________________________________________________ __________________________________________________ ______________
It’s 2013 and I still use printf() for debugging.
You're better off doing the multiply first and so doing it with all integer math:But hey it's not really going to make any important difference here.Code:percent = 100 * marks / 1100;
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"