answersLogoWhite

0


Best Answer

The number of digits is immaterial, the principal is exactly the same. The key to reversing any number is in the following user-defined function:

uint reverse(uint num)

{

uint rev=0;

while(num)

{

rev*=10;

rev+=num%10;

num/=10;

}

return(rev);

}

The following code demonstrates how to reverse a 6 digit number entered from the console.

#include<iostream>

#include<string>

#include<sstream>

typedef unsigned int uint;

uint reverse(uint num)

{

uint rev=0;

while(num)

{

rev*=10;

rev+=num%10;

num/=10;

}

return(rev);

}

int getrange(uint digits,uint& min,uint&max)

{

if(!digits)

return(-1);

min=1;

while(--digits)

min*=10;

max=min*10-1;

return(0);

}

uint inputnumber(const std::string prompt,const uint min,const uint max)

{

using namespace std;

uint result=0;

while(1)

{

cout<<prompt;

string s;

getline(cin,s);

if(s.size())

result=stoul(s,0,10);

if(result<min result>max)

cout<<"The number must be in the range "

<<min<<".."<<max<<"\n"

<<"Please enter another number.\n"<<endl;

else

break;

}

return(result);

}

uint inputnumber(const uint digitcount)

{

using namespace std;

uint min=0, max=0;

if(getrange(digitcount,min,max)==-1)

return(0);

std::stringstream prompt;

prompt<<"Enter a "<<digitcount<<" digit number: ";

return(inputnumber(prompt.str(),min,max));

}

int main()

{

using namespace std;

uint num=inputnumber(6); // 6 digits.

if(num)

{

uint rev=reverse(num);

cout<<"Reverse: "<<rev<<"\n"<<endl;

}

return(0);

}

Output:

Enter a 6 digit number: 123456

Reverse: 654321

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you reverse a 6 digit number in C plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Write a shell script to print given number in reverse order in Unix?

# Algo: # 1) Input number n # 2) Set rev=0, sd=0 # 3) Find single digit in sd as n % 10 it will give (left most digit) # 4) Construct revrse no as rev * 10 + sd # 5) Decrment n by 1 # 6) Is n is greater than zero, if yes goto step 3, otherwise next step # 7) Print rev # if [ $# -ne 1 ] then echo "Usage: $0 number" echo " I will find reverse of given number" echo " For eg. $0 123, I will print 321" exit 1 fi n=$1 rev=0 sd=0 while [ $n -gt 0 ] do sd=`expr $n % 10` rev=`expr $rev \* 10 + $sd` n=`expr $n / 10` done echo "Reverse number is $rev"


EBNF float data type?

&lt;float_literals&gt; -&gt; &lt;digit&gt; { &lt;digit&gt; } [ . ] [ { &lt;digit&gt; } ] &lt;digit&gt; -&gt; "0" | &lt;digit excluding zero&gt; &lt;digit excluding zero&gt; -&gt; "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" hi dude i wish you are satisfied with my answer LOL ^^


What is 10 plus 9 plus 7 plus 6 plus 6 plus 6 plus 5 equals?

It's a simple example of addition.The solution looks like this:10 + 9 + 7 + 6 + 6 + 6 + 5 = 49


Find the missing number 6 3 9 12 21 54 87?

The next number in the sequence is 9. The rule is to add 2, then subtract 1, add 2, subtract 1, and so on.


How do you convert a decimal number to its octal What is the algorithm for it?

Repeatedly divide the number by 8 until the number is zero. Take the remainders from each division (the remainders will always be in the range 0 to 7 inclusive). The first division finds the lowest-order digit, the last finds the highest-order digit. Example: Decimal value: 421 421 / 8 = 52 r 5 52 / 8 = 6 r 4 6 / 8 = 0 r 6 The remainders are 6, 4 and 5, so 421 decimal is 645 octal. To convert from octal to decimal, multiply each octal digit by 8^n, where n is the zero-based order of the digit (0 being the lowest order), then sum the products. Example: Octal number: 645 5 * (8^0) = 5 * 1 = 5 4 * (8^1) = 4 * 8 = 32 6 * (8^2) = 6 * 64 = 384 384 + 32 + 5 = 421 Note that n^0 = 1 for all n&gt;=0.