+ 2
Can someone please correct the code. There's an error in sololearn compiler.
when i enter 657 then it shows wrong output https://code.sololearn.com/cf1pGSJm9YOh/?ref=app
16 Antworten
+ 7
I apologize.
Seems that this is indeed wierd as hell. still think it is doing some truncating but not on pow obviously. but because of the typecast. maybe it is converting the p to a double during the formula.. because 5.99 x 100 would equal the 599 result
+ 6
If I solve it for you, do I get a cookie
??
https://code.sololearn.com/cwwyyCO3N8cb/#cpp
+ 6
@JPM7 - Are we not allowed to use double?
+ 6
@Rishabh: Hey but that wasn't part of the original question...
If you break down the formula in the original you will see it converts a double into an int and as a result some data is lost. thusly in my solution i used a double to store results. Where as JDM removed the pow function and some math to remove the double from the equation entirely.
+ 6
i.e either use a double to store temp or remove it entirely as JDM has. why type cast. you still loose accuracy as the pow formula is always going to return a double. implicitly type casting it inside the formula wont stop the decimal places from getting truncated
+ 6
I already have... see the code I posted and enter a number. It works.
+ 6
but they arent. pow is probably returning 9.9999999
+ 5
ooooo! ok! I see
+ 4
No need to apologize buddy..
it happens sometimes
happy coding...
+ 3
No its returning 100
check my code i have posted as compiler bug
+ 2
but if both are integers i.e. 6*100 it should give 600 not 599?
+ 2
So if i convert 6*100 to int implicitly than rather than getting 600 it gets converted to 599.
That's why you declared temp as double
+ 1
@jpm7
Hey but my question is why i am getting incorrect output in the input condition that I gave?
+ 1
@JAY
#include <iostream>
#include <math.h>
using namespace std;
void express(int p)
{
int n=0,temp;
while(p>0)
{
temp=(p%10)*(int)pow(10,n++);//type casting
if(temp!=0)
{
cout<<temp;
}
p/=10;
if(p!=0 && temp!=0)
cout<<"+";
}
}
int main() {
int a;
cin>>a;
express(a);
}
now the output reduces even more
7+50+594
+ 1
@JAY
just input a number and you will see it
https://code.sololearn.com/cGA5S2L0DhZo/?ref=app