C++ Code to Reverse a Number

Reverse a Number using C++


* Featured Code Example Created by tooselfish.


C++


#include <iostream>


int main() {

    int a, b, c = 0;
    
    // input a whole integer
    // for example: 2485
    std::cin >> b;
    
    // for input failure
    if( std::cin.fail() ){
        std::cerr << "Wrong input!\n";
        return 1;
    }
    
    std::cout << "Your number input : " << b << std::endl;
    
    // reverse the input
    while (b != 0){
        a = b % 10;
        c = ( c * 10 ) + a;
        b = b / 10;
    }
    
    // output the reverse
    std::cout << "The reverse number: " << c << std::endl;
    
    return 0;
}

Click here to run this code



More Free Code Examples:


C++ Code to Reverse a String


C++ Game Guess the Number






Comments

Popular posts from this blog