viewing paste Division by Subtraction in C | C

Posted on the | Last edited on
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include <stdio.h>
 
#define DIV(n,d,q,r) do { (r) = (n), (q) = 0; while ((r) >= (d)) ((r) -= (d), ++(q)); } while(0)
 
int main(void) {
        int numerator, denominator, quotient, remainder;
 
        printf("Input the number you would like to divide: ");
        scanf("%d", &numerator);
 
        printf("Input the number you would like to divide %d by: ", numerator);
        scanf("%d", &denominator);
 
        DIV(numerator, denominator, quotient, remainder);
 
        printf("%d divided by %d equals %d", numerator, denominator, quotient);
        if (remainder) {
                printf(" with a remainder of %d", remainder);
        }
        printf(".\n");
        return 0;
}
 
Viewed 394 times, submitted by datmumbles.