0

enter image description here

code--

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    int x, a, b, c = 0;
    cout << "Enter the number" << endl;
    cin >> x;
    for (int i = 0; i <= 7; i++)
    {
        a = pow(10, I);
        b = 10 * ((x % a) - c) / a;
        cout << b;
        c = x % a;
    }
}

The code is written to reverse the numbers but for 1024 vs code shows 04301000 and onlinegdb shows 04201000.

1 Answers1

0

You don't even need pow:

#include <iostream>

int main()
{
    unsigned int x = 0, y = 0;
    std::cout << "Enter the number: ";
    std::cin >> x;
    while (x > 0) {
        y = 10 * y + (x % 10);
        x /= 10;
    }
    std::cout << y << std::endl;
}

Or if you don't want to remember the result and just print it, like the original:

#include <iostream>

int main()
{
    unsigned int x = 0;
    std::cout << "Enter the number: ";
    std::cin >> x;
    while (x > 0) {
        std::cout << x % 10;
        x /= 10;
    }
    std::cout << std::endl;
}
Goswin von Brederlow
  • 11,875
  • 2
  • 24
  • 42