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

  • C/C++
  • Thread starter zarentina
  • Start date
  • Tags
    Length
In summary, the conversation discusses a programming task involving reversing and calculating the square root of a number. The individual is new to programming and is seeking advice on how to eliminate trailing zeros in the reversed number. Suggestions are made to use string buffers and loops to solve the problem. The individual eventually figures it out using a loop and shares their final code.
  • #1
zarentina
12
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
  • #2
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...
 
  • #3
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?
 
  • #4
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:
 
  • #5
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.
 
  • #6
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
 
  • #7
Looks good! :smile:
 

1. How can I reverse a number in C++?

To reverse a number in C++, you can use a simple algorithm where you extract each digit from the original number using the modulus operator and then append it to a new number by multiplying the new number by 10 and adding the extracted digit. Repeat this process until all digits have been extracted and the new number will be the reversed version of the original number.

2. Can I reverse a number of any length in C++?

Yes, the algorithm for reversing a number in C++ works for numbers of any length. However, for numbers with more than 5 digits, you may need to use a different data type such as a long or a string to store the reversed number, as it may exceed the range of an integer.

3. How do I handle negative numbers when reversing in C++?

When dealing with negative numbers, you can simply ignore the negative sign and reverse the absolute value of the number. Once the reversed number is obtained, you can add back the negative sign to the result if necessary.

4. Is there a built-in function to reverse a number in C++?

No, there is no built-in function in C++ to reverse a number. However, you can create your own function or use a library such as the <algorithm> library which contains a reverse function that can be used to reverse a range of elements, including numbers in an array.

5. Can I reverse a number in C++ without using loops?

Yes, you can use the reverse function from the <algorithm> library to reverse a string representation of the number without using loops. However, this method may not work for numbers with more than 5 digits since it relies on converting the number to a string first.

Similar threads

  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
7
Views
34K
  • Programming and Computer Science
Replies
1
Views
646
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
7
Views
15K
Back
Top