Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number to digits without using strings nor division by 10

So there is this challenge in which you have to write a code which splits a number between 0-999 into digits without using string or division by 10. I've tried so hard and couldn't come up with the perfect algorithm. I got my code working for splitting numbers 1-99 but I really think there's some better alternative without using 111 if statements. Alright, so here's what I got:

#include <iostream>

int main() {
    std::cout << "Enter a number ";
    int number;
    std::cin >> number;

    int cycles;
    if (number > 100) {
        cycles = 3;
    }
    else if (number > 10) {
        cycles = 2;
    }
    else {
        cycles = 1;
    }

    int digit[] = { -1, -1, -1 };
    for (int i = 0; i < cycles; i++) {
        if (number < 10) {
            digit[0] = number;
        }
        else if (number < 100) {
            if (number < 20) {
                digit[1] = number - 10;
                number = 1;
            }
            else if (number < 30) {
                digit[1] = number - 20;
                number = 2;
            }
            else if (number < 40) {
                digit[1] = number - 30;
                number = 3;
            }
            else if (number < 50) {
                digit[1] = number - 40;
                number = 4;
            }
            else if (number < 60) {
                digit[1] = number - 50;
                number = 5;
            }
            else if (number < 70) {
                digit[1] = number - 60;
                number = 6;
            }
            else if (number < 80) {
                digit[1] = number - 70;
                number = 7;
            }
            else if (number < 90) {
                digit[1] = number - 80;
                number = 8;
            }
            else {
                digit[1] = number - 90;
                number = 9;
            }
        }
        else if (number < 1000) {
            if (number < 200) {
                number -= 100;
            }
            else if (number < 300) {
                number -= 200;
            }
            else if (number < 400) {
                number -= 300;
            }
            else if (number < 500) {
                number -= 400;
            }
            else if (number < 600) {
                number -= 500;
            }
            else if (number < 700) {
                number -= 600;
            }
            else if (number < 800) {
                number -= 700;
            }
            else if (number < 900) {
                number -= 800;
            }
            else {
                number -= 900;
            }
        }
    }

    for (int i = 0; i < 3; i++) {
        if (digit[i] != -1) {
            std::cout << digit[i] << " ";
        }
    }
    std::cout << "\n";

    std::cout << "Press any key to exit... ";
    char i;
    std::cin >> i;
    return 0;
}

I'm stuck, so if anyone will be able to help me, that will be much appreciated!

like image 965
user3426112 Avatar asked Feb 09 '23 18:02

user3426112


1 Answers

Basically the problem comes down to, how to implement the division operator without using division operator. As there are quite limited possibilities, i.e, when testing for each digit there are only 10 possible outcomes 0-9, a simple solution is to go through all of them. You could just use multiplication for each, but a slightly faster one is to iterate over the possible answers. So a simple function which takes base (1,10 or 100) and the number you are splitting in parts would be

int getDigit(int base, int number) {
  int digit = 0;
  for (int i = base;i <= number;i += base) ++digit;
  return digit;
}

This function works only if number < 10*base. So you need to start from the largest digit. Then subtract base*digit from the number and repeat until you have gone over all the digits.

like image 83
Ari Hietanen Avatar answered Feb 11 '23 07:02

Ari Hietanen