0

This program basically reverses a number like 4568 to 8654. It is not taking proper input. I know of other ways to do this but could you suggest me how to make this program work.

#include<iostream>

#include<math.h>

using namespace std;

int main()
{
    int num[25];
    int a, b, c, d;

    cout<<"Enter the no. which you want to reverse:\n";
    cin>>num;

    a = (int)sizeof(num)/4;
    
    cout<<"The swapped no. is:\n";

    for (b = a-1; b <= 0; b--)
    {
        cout<<num[b];
    }

    return 0;

}
jiVatx19
  • 21
  • 7

1 Answers1

1

Your code has a few errors.

  1. a = (int)sizeof(c) / 4; will initialize the value of a to be 1 (in platforms where sizeof(int) is equal to 4).
  2. The for loop iterates infinitely. This is because b is initialized to zero (as stated above and 1 - 1 = 0) and the stop condition is set to b <= 0 and since your doing b-- in every iteration, the stop condition will always be true.
  3. cout << num[b]; statement is not going to reverse the input integer which is stored in the c variable. Your just printing out a bunch of elements in the array (which may or may not be 0).

One possible solution would be to use std::string.

#include<iostream>
#include <string>

//using namespace std;  // Try not to use this. (see additional)

int main()
{
    int c;

    std::cout << "Enter the no. which you want to reverse:\n";
    std::cin >> c;

    std::string integerString = std::to_string(c);    // Convert the integer to a string.
    std::string reversed(integerString.rbegin(), integerString.rend());    // Use std::string s reverse iterators to initialize the reversed string.

    std::cout << "The swapped no. is:\n" << reversed;

    return 0;

}

Additional: Why is "using namespace std;" considered bad practice?


Edit: As for your comment, yes we can use some simple maths to get it done.

#include <iostream>

int main()
{
    int c = 0;
    std::cin >> c;

    int biggestPower = 1;
    for (; biggestPower < std::numeric_limits<int>::max(); biggestPower *= 10)
        if (c < biggestPower) break;

    int index = 0;
    int sum = 0;
    int list[25] = {};
    for (; biggestPower > 0 && index < 25; biggestPower /= 10)
    {
        list[index] = (((c / biggestPower) * biggestPower) - sum) / biggestPower;
        sum += ((c / biggestPower) * biggestPower) - sum;
        index++;
    }

    for (index--; index > 0; index--)
        std::cout << list[index];
}

For example, lets use 125 as the input.
We first look for the biggest power of 10 which we can fit in the input value (basically the biggest power of 10 < input value).
Then we can use that to get the three values, { 1, 12, 125 } using the equation input value / powerOf10. But as you can see, we have values like 12 and 125 which is problematic.
To remove this, we multiply the first value, which is 1 in this example by its place value (which is 100) so we get the new list { 100, 120, 125 }. And now we just need to subtract the previous value from the next (apart from the first) and divide it by its place value which gives us the individual numbers { 1, 2, 5 }.
And we can use this to return it in reverse.

D-RAJ
  • 3,263
  • 2
  • 6
  • 24
  • Basically I wanted to ask is there a way to take the input from the user and fill each digit of that integer as the elements of the integer array num so that I could use a 'for' loop and print the array backwards and get the reversed input. I am an absolute beginner so I didn't get this std::string integerString = std::to_string(c); std::string reversed(integerString.rbegin(), integerString.rend()); – jiVatx19 Feb 24 '21 at 10:24