+ 2

Please tell me why this code shows 0 when using int instead of float. Percentage calculator.

https://code.sololearn.com/cKKZnuQ7A961/?ref=app

25th Jan 2021, 3:33 PM
Shubham Dhyani
Shubham Dhyani - avatar
9 Antworten
+ 2
Shubham Dhyani if u take int type then u get zero if your numerator is less than denominator than u will get zero That's why and your obtained mark's will always less than total marks 60/100 here numerator is less than deno... but if u take float or double then suppose 450/500 *100 then u will get 50 but in case of int 450/500 its zero *100
25th Jan 2021, 4:05 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 9
Because decimals will be lost if we use int instead of float. That's why your output is always 0 if marks<totalm.
25th Jan 2021, 3:56 PM
Simba
Simba - avatar
+ 2
Try this #include <iostream> using namespace std; int main() { int marks,totalm; cin>>marks>>totalm ; float perc=(marks*100)/totalm; cout<<perc; }
25th Jan 2021, 3:38 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 2
If marks>total you won't get 0.
27th Jan 2021, 7:55 AM
Sonic
Sonic - avatar
+ 1
Issue is data type u used int put values in your code expression what values u getting and put same values in my expression u will understood better what u getting. here i changed data type in your code it won't effect with float or double but it will give same . put values u will get it #include <iostream> using namespace std; int main() { double marks,totalm; cin>>marks>>totalm ; double perc=(marks/totalm)*100; cout<<perc; }
25th Jan 2021, 3:54 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 1
Simba i put 60 100 it should be 60% but it show 0
25th Jan 2021, 4:02 PM
Shubham Dhyani
Shubham Dhyani - avatar
0
Thabks but why its not working on int why shows 0
25th Jan 2021, 3:47 PM
Shubham Dhyani
Shubham Dhyani - avatar
0
Thanks syrah now i got it
25th Jan 2021, 4:31 PM
Shubham Dhyani
Shubham Dhyani - avatar
0
Now i can continue my journey
25th Jan 2021, 4:32 PM
Shubham Dhyani
Shubham Dhyani - avatar