Getting digits from a number beginning from the least significant in C is pretty easy:
#include
int main()
{
int num = 1024;
while(
Here is my try. Works for positive numbers only. Max range 2^64 (unsigned long long)
#include
#include
using namespace std;
using bignum = unsigned long long;
inline
bignum Power(unsigned x, unsigned y) {
return y>0 ? x*Power(x,y-1) : 1;
}
// return digits count in a number
inline
int Numlen(bignum num) {
return num<10 ? 1 : floor(log10(num))+1;
}
// get the starting divisor for our calculation
inline
bignum Getdivisor(unsigned factor) {
return Power(10, factor);
}
int main()
{
bignum num{3252198};
//cin >> num;
unsigned numdigits = Numlen(num);
auto divisor = Getdivisor(numdigits-1);
while(num > 0) {
cout << "digit = " << (num/divisor) << "\n";
num %= divisor;
divisor /= 10;
}
}
/*
output:
digit = 3
digit = 2
digit = 5
digit = 2
digit = 1
digit = 9
digit = 8
*/