// D test program div_mod_loop.d CSE413 6/99, rev 5/00, rev 11/07 // Software division and mod, using repeated subtraction. // read two positive numbers x and y, then print x/y and x%y // Assumes that negative numbers will not be entered. // Repeatedly loops, enter a zero for the dividend to stop. // = a%b int mod(int a, int b){ return a - div(a,b)*b; } // = a div b for positive integers a, b int div(int a,int b){ int div; div = 0; while (a>b) { a=a-b; div=div+1; } if (a==b) div = div + 1; return(div); } int main(){ int a;int b; int c; c = 0; a = get(); // Get dividend while (!(a == 0)) { b = get(); // Get divisor c = put ( div(a,b) ); // print a / b c = put ( mod(a,b) ); // print a % b a = get(); // Enter 0 to stop } // Returns zero if the last remainder was zero // otherwise return -1. if (c == 0) return 0; else { return 0-1; } }