How do i rearrange the numbres of the value in a variable c++

Rearrange the numbres of the value in a variable c++

How is this done? I tried to google but couldn't really find the right words to google for.

What i mean is that 402 would become 204 and 321 would become 123
Not quite sure what you mean...

I think you want something like this:
1
2
3
4
5
6
7
8
9
10
void num2digits(int num, std::vector<int> &digits)
{
    while (num > 0)
    {
        digits.push_back(num%10);
        num /= 10;
    }
    
    std::sort(digits.begin(), digits.end());
}
Last edited on
You should split the number into digits and then build the reverse number.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

int main()
{
	while ( true )
	{
		const unsigned int base = 10;
		unsigned int x = 0, y = 0;

		std::cout << "Enter a non-negative number (0 - exit): ";
		std::cin >> x;

		if ( !x ) break;

		do { y = base * y + x % base; } while ( x /= base );


		std::cout << "The reverse number: " << y << std::endl;
	}

	return 0;
}
            
Last edited on
Or you can write the code in C# if you do not know how to write it in C++:)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class ReverseNumber
{
	static void Main()
	{
		while ( true )
		{
			const uint base10 = 10;
			uint x = 0, y = 0;			

			System.Console.Write( "Enter non-negative number (0 - exit): " );
			string value;
			value = System.Console.ReadLine();

			x = uint.Parse( value );

			if ( x == 0 ) return;

			do { y = base10 * y + x % base10; } while ( ( x /= base10 ) != 0 );

			System.Console.WriteLine( "The reverse number: " + y );
		}
	}
}
Last edited on

You should split the number into digits and then build the reverse number.





This is working great, however i do not understand the math behind it

let's say we enter 75

by this code

 
do { y = base * y + x % base; } 


we would get this:

y = 10 * 0 + 75 % 10
y = 10 * 0 + 5
y = 5

and this is while x /= base which means 75 = 75 / 10. so right off the bat x does not equal x / base

could you perhaps explain how it works? because i'm at a loss :<

Last edited on
bump
No
do { y = base * y + x % base; }
and this is while x /= base which means 75 = 75 / 10.

% is the modulo operator which is different from the divisor operator. % is a calculation of the positive remainder when u continuously subtract x until the new x is between 0 and the base. Another example is:

19%3 = 1
because
19-3 = 16
16-3 = 13
13-3 = 10
10-3 = 7
7-3 = 4
4-3 = 1 <-----and 1 is between 0 and 3 so we stop here
Last edited on

No
do { y = base * y + x % base; }
and this is while x /= base which means 75 = 75 / 10.

% is the modulo operator which is different from the divisor operator. % is a calculation of the positive remainder when u continuously subtract x until the new x is between 0 and the base. Another example is:

19%3 = 1
because
19-3 = 16
16-3 = 13
13-3 = 10
10-3 = 7
7-3 = 4
4-3 = 1 <-----and 1 is between 0 and 3 so we stop here


i'm sorry but it does still not make any sense to me so with the % operator the program would calculate (let's say 75 again) like this:

75-10 = 65
65-10 = 55
..
15 - 10 = 5

and so we still end up with

y = 10 * 0 + 5

and the while loop would still not be true since x = x / base does not = x


Last edited on
The thing you don't seem to be getting is that the loop doesn't stop executing after one iteration.

And by the way the loop condition is x /= base. It is not x == x/base, which is what you seemed to be implying in your last post.

Try this modification of Vlad's code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>

int main()
{
    while ( true )
    {
        const unsigned int base = 10;
        unsigned int x = 0, y = 0;

        std::cout << "Enter a non-negative number (0 - exit): ";
        std::cin >> x;

        if ( !std::cin || !x ) 
            break;

        std::cout << "y=" << y << "\tx=" << x << '\n' ;

        do 
        { 
            y = base * y + x % base; 
            x /= base ;

            std::cout << "y=" << y << "\tx=" << x << '\n' ;
        } while ( x );


        std::cout << "The reverse number: " << y << std::endl;
    }

    return 0;
}
Last edited on
@cire

There is no need to make the condition more complicated because if an input error will occur x will be equal to 0 that is x will not be changed.
So instead of

1
2
if ( !std::cin || !x ) 
            break;



it is simpler to write

if ( !x ) break;

@Serri
and this is while x /= base which means 75 = 75 / 10


In C/C++ any expression in a condition that is differing from zero is converted to bool value true and if it is equal to zero then converted to false.

The expression

x /= base;

is equivalent to

x = x / base;

x /= base; is just a simplified form of x = x / base;
So the result of x /= base; when x = 75 will be equal to 7. 7 differs from 0 so in the condition it is converted to true and the loop will be repeated until value of x will be equal to 0.
Last edited on
There is no need to make the condition more complicated because if an input error will occur x will be equal to 0 that is x will not be changed.


Being explicit is sometimes more useful than being efficient.
ok yeah so i think i get it..

first it goes

y = 10 * 0 + 75 % 10
y = 5

x = 75 / 10 = 7,5

y = 10*5 + 7 % 10 = 57

question is does the x = 75 / 10 = 7,5 part automatically remove 0,5 since it's an interger and not a double?

also what happens when you write while ( x ) when does the loop end?
Last edited on
That's correct. It's called integer division and it's perfect for these situations.

PS. You're probably beyond it now, but this might help:
1
2
3
4
5
6
7
8
9
10
11
12
int reverse( int n )
{
  int output = 0;
  
  while (n > 0)
  {
    output = output * 10 + n % 10;// output *= 10; output += n % 10; <-- this would work too.
    n /= 10;
  }
  
  return output;
}
Here's something recursive. My implementation didn't have int pow(int,int) or int log10(int,int) so I made recursive versions of those too:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int pow(int base, int exp)
{
  if (exp) return base * pow(base,exp-1);
  return 1;
}

int log10(int n)
{
  if (n) return 1+log10(n/10);
  return 0;
}

int reverse(int n)
{
  if (n) return reverse(n / 10) + n%10 * pow(10,log10(n)-1);
  return 0;
}
Last edited on
Topic archived. No new replies allowed.