top of page
Writer's pictureThe Tech Platform

Write C++ Program to Reverse the Digits of given Integer.


#include <iostream>
using namespace std;

    int reverse_int(int x) {
        
        int res = 0;
        while(x != 0)
        {
            int pop = x % 10;
            x = x / 10;

            int candidate = res * 10 + pop;

            if (candidate / 10 != res)
            {
                return 0;
            }

            res = candidate;            
        }

        return res;
    }

int main() {
cout<<"Reverse of the Digits of Given Integer"<<endl;
	cout << "Reverse of 49 is " << reverse_int(49) << endl;
	cout << "Reverse of 896 is " << reverse_int(896) << endl;
	cout << "Reverse of 6489235 is " << reverse_int(6489235) << endl;
	cout << "Reverse of -6565 is " << reverse_int(-6565) << endl;

	return 0;
}

Output:



Sofia Singh

The Tech Platform

0 comments

Comments


bottom of page