Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Oct 1, 2011 #1
    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 wont throw off the square root of the reversed number.

    Thanks,
    Z
     
  2. jcsd
  3. Oct 1, 2011 #2

    I like Serena

    User Avatar
    Homework Helper

    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...
     
  4. Oct 1, 2011 #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?
     
  5. Oct 1, 2011 #4

    I like Serena

    User Avatar
    Homework Helper

    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:
     
  6. Oct 1, 2011 #5

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    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.
     
  7. Oct 1, 2011 #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
     
  8. Oct 2, 2011 #7

    I like Serena

    User Avatar
    Homework Helper

    Looks good! :smile:
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: New to C++; reversing a number upto 5 digits in length
  1. Reverse the number (Replies: 0)

  2. Java- reverse sentence (Replies: 41)

Loading...