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
# 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"
<float_literals> -> <digit> { <digit> } [ . ] [ { <digit> } ] <digit> -> "0" | <digit excluding zero> <digit excluding zero> -> "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" hi dude i wish you are satisfied with my answer LOL ^^
It's a simple example of addition.The solution looks like this:10 + 9 + 7 + 6 + 6 + 6 + 5 = 49
The next number in the sequence is 9. The rule is to add 2, then subtract 1, add 2, subtract 1, and so on.
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>=0.
261, 264, or 267
The greatest six-digit number is 199,999.
what is the difference between the largest 8-digit number and the smallest 6-digit number
The last digit: the 6.The last digit: the 6.The last digit: the 6.The last digit: the 6.
The greatest six-digit number is 199,999.
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.
999,999 is 6 digits Adding 1 makes it 1,000,000 = a 7 digit number.
987654
60 is the number in which the digit 6 have a greater value as compared to 106.
0
Take the smallest 6-digit even number, then subtract one from it.
The 6