Thursday, 17 July 2014

Three times the sum of digits of the number equals number itself.

// num = 27 = 3*(2+7)
// AIM: To find all such numbers

#include<stdio.h>
void main()
{
    long num = 1, i;
    int temp = 0;
    long  test = 0;
    long count = 0;
    while(count < 100)
    {
        i = num;
        test = 0;
        while(i != 0)
        {
            temp = i % 10;
            test = test + temp;
            i = i / 10;
        }
        if( (test*3) == num)
        {
            printf("found : %d", num);
            //only such number is 27, u can check upto infinite but no use, I already tried. :P
        }
        //printf("num = %d and test*3 = %d\n",num, test*3);
        count++;
        num++;
    }
}