A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

Write a C++ program to reverse the digits of a given integer
Q:

Write a C++ program to reverse the digits of a given integer

0

Write a C++ program to reverse the digits of a given integer

Sample Input: 4
Sample Output: 4

Sample Input: 123
Sample Output: 321

Sample Output:

Reverse of 4 is 4
Reverse of 123 is 321
Reverse of 208478933 is 339874802
Reverse of -73634 is -43637

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#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 4 is " << reverse_int(4) << endl;
	cout << "Reverse of 123 is " << reverse_int(123) << endl;
	cout << "Reverse of 208478933 is " << reverse_int(208478933) << endl;
	cout << "Reverse of -73634 is " << reverse_int(-73634) << endl;

	return 0;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now