Answer to Question #333289 in C++ for hackman

Question #333289

Write a C++ Function program that takes up to 10-digit integer input from user (can be 1 digit, 2 digit, and

so on..); passes this to a function which reverses the digits. Finally, the reversed number should

be displayed in the main function. For example: when user enters 10-digit like 1234567890 your

function will reverse it to 987654321. Do not use strings. Be careful for big integer values. [use

functions, decision control]


1
Expert's answer
2022-04-26T09:27:41-0400
#include <iostream>
#include <cmath>

using namespace std;

long int ReverseDigit(long int n)
{
	long int res=0;
	int k = n;
	int i = -1;
	while (k > 0)
	{
		k /= 10;
		i++;
	}
	while (n > 0)
	{
		res += (n % 10)*pow(10, i--);
		n /= 10;
	}


	return res;
}

int main()
{
	long int n;
	cout << "Please, enter a digit: ";
	cin >> n;
	cout << "Resulting number is ";
	cout << ReverseDigit(n);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS