#include <iostream>
int main()
{
while ( true )
{
constunsignedint base = 10;
unsignedint 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;
}
class ReverseNumber
{
staticvoid 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 );
}
}
}
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
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
#include <iostream>
int main()
{
while ( true )
{
constunsignedint base = 10;
unsignedint 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;
}
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.
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;
}