rotateNumber

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
719 B
13
Indexable
Never
unsigned int myPow(unsigned int base, unsigned int expo){
    if (expo == 0) return 1;
    unsigned int result = base;
    for (unsigned int i = 1; i < expo; i++){
        result *= base;
    }
    return result;
}
unsigned int numberLength(unsigned int num){
    int digits = 0;
    do {
        digits++;
        num /= 10;
    }while (num > 0);
    return digits;
}

unsigned int rotateNumber(unsigned int num, unsigned int spins){
    unsigned length = numberLength(num);
    spins %= length;
    unsigned int midPoint = myPow(10, spins);
    unsigned int rightSide = num % midPoint;
    unsigned int leftSide = num / midPoint;
    rightSide *= myPow(10, numberLength(leftSide));
    return rightSide + leftSide;
}