开发者

reverse the position of integer digits?

i have to reverse the position of integer like this

input = 12345

output = 54321

i made thi开发者_JS百科s but it gives wrong output e.g 5432

#include <iostream>
using namespace std;

int main(){
 int num,i=10;   
 cin>>num;   

 do{
    cout<< (num%i)/ (i/10);
    i *=10;
   }while(num/i!=0);

 return 0;
}


Here is a solution

    int num = 12345;
    int new_num = 0;
    while(num > 0)
    {
            new_num = new_num*10 + (num % 10);
            num = num/10;
    }
    cout << new_num << endl;


Your loop terminates too early. Change

}while(num/i!=0);

to

}while((num*10)/i!=0);

to get one more iteration, and your code will work.


If you try it once as an example, you'll see your error.

Input: 12

first loop:

out: 12%10 = 2 / 1 = 2
i = 100
test: 12/100 = 0 (as an integer)

aborts one too early.

One solution could be testing

(num % i) != num

Just as one of many solutions.


Well, remember that integer division always rounds down (or is it toward zero?) in C. So what would num / i be if num < 10 and i = 10?


replace your while statement

with

while (i<10*num)


If I were doing it, I'd (probably) start by creating the new value as an int, and then print out that value. I think this should simplify the code a bit. As pseudocode, it'd look something like:

output = 0;

while (input !=0)
    output *= 10
    output += input % 10
    input /= 10
}
print output

The other obvious possibility would be to convert to a string first, then print the string out in reverse:

std::stringstream buffer;

buffer << input;

cout << std::string(buffer.str().rbegin(), buffer.str().rend());


int _tmain(int argc, _TCHAR* argv[])
{
int x = 1234;
int out = 0;
while (x != 0)
{
    int Res = x % (10 );
    x /= 10;
    out *= 10;
    out +=  Res;
}
cout << out;


} 


This is a coding assignment for my college course. This assignment comes just after a discussion on Operator Overloading in C++. Although it doesn't make it clear if Overloading should be used for the assignment or not.


The following code works for a two-digit number only.

#include<iostream>
using namespace std;

int main() {
    int n;
    cin >> n;
    cout << (n%10) << (n/10);    
return 0;
}


int a,b,c,d=0;
cout<<"plz enter the number"<<endl;
cin>>a;
b=a;
do
{
    c=a%10;
    d=(d*10)+c; 
    a=a/10;
}
while(a!=0);
cout<<"The reverse of the number"<<d<<endl;
if(b==d)
{
    cout<<"The entered number is palindom"<<endl;
}
else
{
    cout<<"The entered number is not palindom"<<endl;
}

}


template <typename T>
T reverse(T n, size_t nBits = sizeof(T) * 8)
{
    T reverse = 0;
    auto mask = 1;

    for (auto i = 0; i < nBits; ++i)
    {
        if (n & mask)
        {
            reverse |= (1 << (nBits - i - 1));
        }
        mask <<= 1;
    }

    return reverse;
}

This will reverse bits in any signed or unsigned integer (short, byte, int, long ...). You can provide additional parameter nBits to frame the bits while reversing.

i. e. 7 in 8 bit = 00000111 -> 11100000 7 in 4 bit = 0111 -> 1110


public class TestDS {

public static void main(String[] args) {

    System.out.println(recursiveReverse(234));
           System.out.println(recursiveReverse(234 ,0));



}



public static int reverse(int number){

    int reversedNumber = 0;
    int temp = 0;


    while(number > 0){

        //use modulus operator to strip off the last digit
        temp = number%10;

        //create the reversed number
        reversedNumber = reversedNumber * 10 + temp;
        number = number/10;

    }



    return reversedNumber;

}



private static int reversenumber =0;
public static int recursiveReverse(int number){

    if(number <= 0){

        return reversenumber;
    }

    reversenumber = reversenumber*10+(number%10);
    number =number/10;

    return recursiveReverse(number);

}

public static int recursiveReverse(int number , int reversenumber){

    if(number <= 0){

        return reversenumber;
    }

    reversenumber = reversenumber*10+(number%10);
    number =number/10;

    return recursiveReverse(number,reversenumber);

}

}


I have done this simply but this is applicable upto 5 digit numbers but hope it helps

 #include<iostream>
    using namespace std;
void main()
{ 
    int a,b,c,d,e,f,g,h,i,j;
    cin>>a;
    b=a%10;
    c=a/10;
    d=c%10;
    e=a/100;
    f=e%10;
    g=a/1000;
    h=g%10;
    i=a/10000;
    j=i%10;
    cout<<b<<d<<f<<h<<j;
}`
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜