the following code takes in any number as input.Then re-forms the same number digit by digit and (should) produces the same number as output.But due to some reason the value of the number reduces by 1 after every third itteration of the loop.Can someone please expain why this is happening...(for eg-- 123 is becoming 122 and similarly 123456 is becoming 123454 (why ?)...Here is my code
<code>
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int inp;
cout<<"\n ENTER NUMBER :\t";cin>>inp;
int num=inp,digi_num=0,i=0;
while(inp>0)
{
digi_num+=(pow(10,i))*(inp%10);
inp=inp/10;
i++;
}
cout<<"\n"<<digi_num;
return 0;
}
</code>
> But due to some reason the value of the number reduces by 1
The function std::pow() performs a computation on floating point values and yields a floating point value as the result. If getting a guaranteed exact integer result is important, do not use floating point operations.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
int main()
{
unsignedint number ;
std::cout << "enter number :\t";
std::cin >> number ;
unsignedint digi_num = 0 ;
for( unsignedint pow10 = 1 ; number > 0 ; pow10 *= 10 )
{
digi_num += pow10 * (number%10) ;
number /= 10 ;
}
std::cout << digi_num << '\n' ;
}