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

  • Context: C/C++ 
  • Thread starter Thread starter zarentina
  • Start date Start date
  • Tags Tags
    Length
Click For Summary

Discussion Overview

The discussion revolves around writing a C++ program to reverse a number up to 5 digits in length and calculate the square roots of both the original and reversed numbers. Participants explore different coding approaches and techniques to address issues related to handling numbers with fewer than 5 digits.

Discussion Character

  • Technical explanation
  • Exploratory
  • Homework-related

Main Points Raised

  • One participant shares their initial code and expresses difficulty with trailing zeros when reversing numbers with fewer than 5 digits.
  • Another participant suggests using a string buffer to reverse the number and convert it back to an integer, indicating that C++ supports various methods for this.
  • A further request for clarification leads to questions about converting numbers to strings, reversing strings, and converting strings back to numbers.
  • Another participant proposes using a loop to reverse the number instead of separate variables for each digit, providing a conceptual outline of the loop's logic.
  • The original poster later reports successfully implementing a loop to reverse the number and shares their revised code for feedback.
  • A final response offers positive feedback on the revised code.

Areas of Agreement / Disagreement

Participants generally agree on the effectiveness of using a loop for reversing the number, but there are multiple approaches suggested, including using strings, indicating that no single method is established as the best practice.

Contextual Notes

Some participants' suggestions depend on familiarity with the Standard C++ Library and string manipulation, which may not be universally understood by all beginners.

Who May Find This Useful

Newcomers to C++ programming, particularly those learning about number manipulation and basic algorithms.

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, count
#include<cmath>
using namespace std;
int main()

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



count<< "Please enter an integer, up to 5 digits in length" <<endl<<endl;
cin>>num;
count<< "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

count<< "The number in reverse is:" <<num2<<endl;
count<< "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, count
#include<cmath>
using namespace std;
int main()

{
int num;

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

int r = num;
int rev = 0;

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

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

system("pause");
return 0;
}


Thanks again,
Z
 
Looks good! :smile:
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
35K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
Replies
7
Views
16K
Replies
22
Views
5K
  • · Replies 7 ·
Replies
7
Views
3K
Replies
4
Views
4K