Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move the first digit to the end of a number in c++?

Tags:

c++

Here is the code:

#include <iostream>
using namespace std;

int main ()
{
    int n;
    cin >> n;
    int first = n;

    while (first>=10)
    {
      first/=10;
    }

    cout << first << endl;
}

In the above code that I tried to get the first digit of a positive number, what I want to do is to put it after the last digit like this, for example: 1934 -> 9341.

like image 269
Derek Cole Avatar asked Feb 02 '18 13:02

Derek Cole


People also ask

How do you return the first digit of a number?

To find first digit of a number we divide the given number by 10 until number is greater than 10. At the end we are left with the first digit.

How do you get the last digit of a number C?

To find the last digit of a number, we use modulo operator %. When modulo divided by 10 returns last digit of the input number.

How do you swap digits in a number?

That is, to interchange, it must be a two-digit or more than two-digit number. If the given number is a two-digit number, then simply reverse the number. For example, if the number is 12 then after reversing, it will become 21.


2 Answers

Convert the number to a string using std::to_string, perform the left rotation using std::rotate and convert back to a number using std::stoull function:

std::string s = std::to_string(n);
std::rotate(s.begin(), s.begin() + 1, s.end());
n = std::stoull(s);

With all the headers included:

#include <iostream>
#include <string>
#include <algorithm>

int main() {
    unsigned long long n = 1934;
    std::string s = std::to_string(n);
    std::rotate(s.begin(), s.begin() + 1, s.end()); // left rotation
    n = std::stoull(s);
    std::cout << n; // 9341
}
like image 156
Ron Avatar answered Oct 07 '22 12:10

Ron


Here is a simple solution that does not use strings or floating point functions / arithmetic. The usage of functions such as pow() may run into problems as outlined in this question.

 #include <iostream>

 int main()
 {
    unsigned long long n = 1934L;

    // save the original
    unsigned long long final_number = n;

    // multiplying factor
    unsigned long long mult = 1;

    // start by making sure we do not loop one too many times to 
    // calculate the multiplier
    n /= 10;

    while (n > 0)
    {
        // determines the multiplication factor after the loop 
        mult *= 10; 

        // strip off digit from number
        n /= 10;
    }

    // create the final number from the original and the multiplication factor
    final_number = (final_number % mult) * 10 + final_number / mult;
    std::cout << final_number << "\n";
}

Live Example

Basically we count how many digits by looping, and at the same time increase the multiplying factor by 10. Then after the loop, the number is constructed by using modulus, multiplication, division, and addition.

So for example, after the loop, the final_number would be

(1934 % 1000) * 10 + 1934 / 1000 =
934 * 10 + 1934 / 1000 = 
9340 + 1934 / 1000 =
9340 + 1 =
9341

Note: I looked at the generated assembly language here and was amazed that the compiler is able to figure out the intent of the code, and computed 9341 at compile-time. I doubt the pow solutions or floating point methods would produce these results.

like image 28
PaulMcKenzie Avatar answered Oct 07 '22 10:10

PaulMcKenzie