answersLogoWhite

0

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

12y ago

What else can I help you with?

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"


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.


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


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.

Related Questions

I am a 3 digit number I am divisible by 3 My hundreds digit is one third my tens digit If you reverse me i am divisible by 6 what am i?

261, 264, or 267


What is greatest 6-digit number?

The greatest six-digit number is 199,999.


What is the difference in value between the largest 6 digit number and the smallest 8 digit number?

what is the difference between the largest 8-digit number and the smallest 6-digit number


Which digit in the following number is the one that determines the precision 183.6?

The last digit: the 6.The last digit: the 6.The last digit: the 6.The last digit: the 6.


What is 6 digit number?

The greatest six-digit number is 199,999.


What is the smallest 6 digit number using the digits 12345?

Since there are only five different digits, a 6-digit number can only be generated if a digit can be repeated. If digits can be repeated, the smallest 6-digit number is 111111.


What is a 6 digit number when we add 1 to it becomes a 7 digit number?

999,999 is 6 digits Adding 1 makes it 1,000,000 = a 7 digit number.


What is the greatest 6 digit even number with no repeated digit number?

987654


What is the sum of the smallest 6-digit even number and the largest 6-digit odd number?

0


In which number does the digit 6 have a greater value?

60 is the number in which the digit 6 have a greater value as compared to 106.


What is the predecessor of smallest 6 digit even number?

Take the smallest 6-digit even number, then subtract one from it.


9 plus 3 equals 722 5 plus 7 equals 536 6 plus 8 equals 847 7 plus 2 equals what ans will come?

Answer is 411 Logic: You multiply the two numbers in LHS of the equation and reverse the result to form the first two digits of RHS and for the 3rd digit you subtract one from the 2nd number on the LHS of the equation.