Search code examples
c++calgorithmlogic

Digit-increasing number test


A number is called digit-increasing if it is equal n + nn + nnn + ... for some digit n between 1 and 9. For example 24 is digit-increasing because it equals 2 + 22 (here n = 2).

Actually, a friend of mine asked me this question and i am stuck thinking about it but couldn't find the exact solution so far. Can anyone help ? I needed the function that returns true if it is digit-increasing else false.


Solution

  • I have done in this way. Check out once.

    int sum = 0, count =0;
    bool flag = false;
    
    public bool isDigitIncreasing(int input_number)
    {
    int  n= get_number_of_digit(input_number); // Gets number of digits
    int sum = 0;
        for(int i=0;i<n;i++)
        {
            sum = sum*10+1;
            count = count + sum;
        }
    
        for(int i=1; i<=9;i++)
        {
            if((input_number)==count*i)
            {
                flag = true;
                break;
            }
            else
            flag = false;
        }
        return flag;
    
    }
    
        public int get_number_of_digit(int num)
        {
    
            int size = 0;
            do
            {
                num = num/10;
                size++;
            }while(num>0);
            return size;
        }