+ 2
Please tell me why this code shows 0 when using int instead of float. Percentage calculator.
9 Answers
+ 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
+ 9
Because decimals will be lost if we use int instead of float.
That's why your output is always 0 if marks<totalm.
+ 2
Try this
#include <iostream>
using namespace std;
int main()
{
int marks,totalm;
cin>>marks>>totalm ;
float perc=(marks*100)/totalm;
cout<<perc;
}
+ 2
If marks>total you won't get 0.
+ 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;
}
+ 1
Simba i put 60 100 it should be 60% but it show 0
0
Thabks but why its not working on int
why shows 0
0
Thanks syrah now i got it
0
Now i can continue my journey