answersLogoWhite

0

How do you reverse a 5 digit number in c plus plus?

Updated: 8/20/2019
User Avatar

Wiki User

11y ago

Best Answer

It really does not matter how many digits there are, the principal is exactly the same. The following function is the most efficient way to reverse any whole number in the range -2,147,483,648 to 2,147,483,647, inclusive (assuming a 32-bit integer). To increase the range, create separate functions to cater for 64-bit and 128-bit integers. Converting the number to a string and reversing the string is also an option (see previous answer, below), however it is by far the least efficient method of reversing a whole number.

int RevNum( int num )

{

const int base = 10;

int remain = 0;

int result = 0;

do

{

remain = num % base;

result *= base;

result += remain;

} while( num /= base);

return( result );

}

Previous Answer

This is assuming the number is decimal.

There are several approaches to the problem, depending on the resources you have available or wish to use to solve it.

One way to achieve this is to isolate the digits through modulo division and the remultiply them in the desired order:

rev = ((original % 10) * 10000) + (((original / 10) % 10) * 1000) + (((original / 100) % 10) * 100) + (((original / 1000) % 10) * 10) + (original / 10000);

An alternative that doesn't look so messy:

int temp = original;

int rev = 0;

for (int i=0;i<5;i++)

{

rev = (rev * 10) + (temp % 10);

temp /= 10;

}

If you're employing strings, then another novel way can be used.

#include

#include

#include

char numstring[6];

snprintf(numstring,5,"%d",original);

strrev(numstring);

rev = atoi(numstring);

Note that these are just examples of how it can be achieved. There is no standard function to reverse a decimal number in C or C++, so it's up to you to find or code a solution for yourself.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

It doesn't matter how many digits are involved, the principal is the same.

int RevNum( int num )

{

const int base = 10;

int result = 0;

int remain = 0;

do

{

remain = num % base;

result *= base;

result += remain;

} while( num /= base);

return( result );

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you reverse a 5 digit number in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What 5 digit number 4 equals itself in reverse?

44444


What 5 digit number multiplied by 4 equals the same number in reverse?

00000


What is the 5 digit number that if you multiply with 4 you can get the reverse number of it and start with number 2?

21978


What is the 3rd digit of the number 99324 then plus it with 2?

99324 (the third digit is 3)3+2=5


A five digit number which the first digit plus the third digit equal to fourteen?

try 91511, 9+5=14


What number am I. I am a two digit number You can count to me by 4's plus 3 or by 5's plus 3 What is the smallest number i can be?

23


What 5 digit number when multiplied by 4 yields the number the same five digits in reverse order?

The number is 21978. 21978 when multiplied by 4 which gives the result 87912 which is in reverse order.


Reverse the order of separate numbers without using array or list exinput 5 7 9 1 output 1 9 7 5?

main() { int no,reverse=0,digit; scanf("%d",no); for( ;no&gt;0; ) { digit=no%10; reverse=reverse*10 + digit; no=no/10; } printf("%d",reverse); }


What is the smallest tree-digit number that has the digit 5?

105 - is the smallest tree-digit number that has the digit 5


Explain why 45 is not a prime number?

Any number that ends with the digit 5 is divisible by 5.Any number that ends with the digit 5 is divisible by 5.Any number that ends with the digit 5 is divisible by 5.Any number that ends with the digit 5 is divisible by 5.


What is an communitive property of addition?

The commutative property of addition states that a number plus another number equals the same number plus the same number in reverse order. An example of this is 2+5=5+3.


What is the smallest 5 digit number?

The smallest, positive 5-digit whole number is 10,000