C/C++ New to C++; reversing a number upto 5 digits in length

  • Thread starter Thread starter zarentina
  • Start date Start date
  • Tags Tags
    Length
AI Thread Summary
A user new to C++ sought help reversing a number up to five digits and calculating square roots. They initially faced issues with trailing zeros in reversed numbers affecting calculations. Suggestions included using a string buffer for reversal and utilizing loops instead of multiple variables. The user successfully implemented a loop to reverse the number, improving their code. The final solution efficiently calculates the square root of both the original and reversed numbers without trailing zero issues.
zarentina
Messages
12
Reaction score
0
Hello all, I hadn't touched a bit of programing until the start of this semester and it feels as if I've been thrown to the wolves. I am required to write a program that takes UP TO 5 digits, reverses it, and calculate the square roots of both. The code that I have thus far is;

#include<iostream> //required for cin, cout
#include<cmath>
using namespace std;
int main()

{
int n1,n2,n3,n4,n5,num;



cout<< "Please enter an integer, up to 5 digits in length" <<endl<<endl;
cin>>num;
cout<< "The Square root is:"<<sqrt(num)<<endl<<endl;
n5=num/10000; //
n4=(num%10000)/1000; //
n3=((num%10000)%1000)/100; //
n2=(((num%10000)%1000)%100)/10; //
n1=((((num%10000)%1000)%100)%10)/1; //

int num2=((n1*10000)+(n2*1000)+(n3*100)+(n4*10)+(n5*1)); // to declare second intiger

cout<< "The number in reverse is:" <<num2<<endl;
cout<< "The square root is:" <<sqrt(num2)<<endl;

system("pause");
return 0;
}

My only problem is with numbers that are less than 5 digits in length, when I have a number such as 234 when it reverses it the output sees it as 43200. How do I eliminate the trailing zeros so it won't throw off the square root of the reversed number.

Thanks,
Z
 
Technology news on Phys.org
Welcome to PF, zarentina! :smile:

Perhaps you could put your number in a string buffer, reverse the string buffer, and convert it to an integer?
C++ supports various ways to do this...
 
Thanks for the reply,

Could you further explain what that means or how I would go about doing it? I guess I didn't iterate that I am completely terrible and 100% lost when it comes to writing code. I'm not fishing for an answer as I have no qualms with doing the work on my own just a simple nudge in the right direction?
 
Well, the nudge is to take a look at the Standard C++ Library.
What kind of materials do you have to look stuff up on C++?
And do you know what the word "string" means in the context of programming?

The questions are:
1. How do you convert a number to a string?
2. How do you reverse the order of the characters in a string?
3. How do you convert a string to a number?

You could even google these questions! :wink:
 
Or, use a loop rather than separate variables for the 5 digits.

if n is the number and r is the reversed number, you could write a loop to do something like this:

Suppose you start with n = 1234 and r = 0
The first time through the loop, find the last digit of n (which is 4) and then make n = 123 and r = 4
The second time through, the last digit will be 3, and then make n = 12 and r = 43
Then n = 1 and r = 432
Then n = 0 and r = 4321
And exit the loop when n = 0

Work out the details for yourself. You will probably want to use another variable as a temporary place to remember the each "last digit" of n. But you only need one variable not five, because once you have updated r you don't need to remember the "last digit" and longer.
 
I just wanted to thank you for the replies to this thread, I figured it out using a loop in place of separate variables. This was my final product, feel free to critique if necessary!


#include<iostream> //required for cin, cout
#include<cmath>
using namespace std;
int main()

{
int num;

cout<< "Please enter an integer, up to 5 digits in length" <<endl<<endl;
cin>>num;
cout<< "The Square root is:"<<sqrt(num)<<endl<<endl;

int r = num;
int rev = 0;

while (r>0)
{
rev = (rev * 10) + (r % 10);
r /= 10;
}

cout<< "The number in reverse is:" <<rev<<endl;
cout<< "The square root is:" <<sqrt(rev)<<endl;

system("pause");
return 0;
}


Thanks again,
Z
 
Looks good! :smile:
 

Similar threads

Back
Top