Monday 7 April 2014

Program to check whether x ( 2 <= x <= 10^100) is divisible by y ( 2 <= y <= 10^6) .

/*
  * Problem : Given 2 number x and y where  2 <= x <= 10^100 and  2 <= y<= 10^6.
  * Algorithm : Long division Method.
  * Variables:
  *  dividend - Each element of dividend aray represent a digit of x. dividend[0] = Most significant Digit
  *  divisor  - y
  *  size     - No. of digits in x
  * If x is divisible by y then return true else return false.
*/
#define len 1000000

bool divide(int dividend[], int divisor, int size) {
    int rem, d, index;
    rem = d = index = 0;
    while(true) {
        d = rem;
        while( d < divisor && index < size ) {
            d = d*10 + dividend[index++];
        }
        rem = d % divisor;
        if(index >= size) {
            if(rem == 0) {
                return true;
            }else {
                return false;
            }
        }
    }
}