I have been trying to implement something in C++ but apparently, there's a syntax error.
The following code yields "1 3100" when 31 is entered as input :
#include<iostream> #include<cmath> using namespace std; int main() { long long n; cin>>n; long long j = floor((log10(n))); long long nn = (n*((long long)pow(10,j+1)))+n; cout<<j<<" "<<nn; }
The following code yields "1 3130" for the same input, i.e, 31 :
#include<iostream> #include<cmath> using namespace std; int main() { long long n; cin>>n; long long j = floor((log10(n))); long long nn = (n*(pow(10,j+1)))+n; cout<<j<<" "<<nn; }
And I wished to produced "1 3131" for the input 31. Basically, I am trying to write the number twice in a row: the same thing that you get when you parse the number into string and add the same string twice (like, n=11, parse into s = "11" and then yield s+s).
So I want to multiply the input by a suitable power of ten to get enough "trailing zeros" and then add the input again.
Where am I going wrong? Also, why is there a difference between the two codes above? (Please explain why the first code gives that as an output and the second code that as an output and also help me with a newer code to get the desired output).