reverse the position of integer digits?

后端 未结 13 2513
予麋鹿
予麋鹿 2021-02-09 15:00

i have to reverse the position of integer like this

input = 12345

output = 54321

i made this but it gives wrong output e.g 5432

#include         


        
相关标签:
13条回答
  • 2021-02-09 15:29

    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;
    }
    
    0 讨论(0)
  • 2021-02-09 15:31

    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());
    
    0 讨论(0)
  • 2021-02-09 15:32

    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.

    0 讨论(0)
  • 2021-02-09 15:33

    replace your while statement

    with

    while (i<10*num)
    
    0 讨论(0)
  • 2021-02-09 15:37
    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;
    }
    

    }

    0 讨论(0)
  • 2021-02-09 15:37

    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);
    
    }
    

    }

    0 讨论(0)
提交回复
热议问题