+ 1

I have a problem with solving this problem...

There is a program to write this: when I input some natural numbers X, N and K, then I have to find the last K digits of number X^N. Now, I don't really know how to store those K digits and then print them...

18th Mar 2019, 6:52 PM
kantarevic7
kantarevic7 - avatar
4 Respostas
+ 1
I would do smthg like : #include <string> std::string s = std::to_string(X^N); Then take the last K characters of that string
18th Mar 2019, 7:06 PM
Dyf Dyf
Dyf Dyf - avatar
+ 1
Hi. First you have to calculate x^n by pow (x,y) and save it in string format then if it lengths is equal or grather than k return this section by substr(savedStr.length-k.length) function.
18th Mar 2019, 7:18 PM
Vahid Vakili
Vahid Vakili - avatar
+ 1
Thank you both. I solved this with for-loop like for(int i = 0; i < K; i++) { F[i] = number % 10; number /= 10; reversednumber = reversednumber * 10 + F[i]; } The name number is actually that X^N. Then I converted that reversednumber into string and reversed it again, because of loaded zeros. It works for me perfectly. Maybe it is a bit complicated but it works haha.
18th Mar 2019, 8:51 PM
kantarevic7
kantarevic7 - avatar
+ 1
As long as you understand what you did and why you did it it's perfectly fine (at first) we all got our own way of thinking and that's why there is a bunch of way to resolve the same problem But sharing your code and/or coding with other is a good way to improve one skill and learn new ways to improve and optimize one's code (never forget that coding means we have to evolve constantly)
19th Mar 2019, 3:45 AM
Dyf Dyf
Dyf Dyf - avatar