0

I am working on division in C, I have 2 ints which are n and p. I want to divide n by p (n/p) and always want it to round up. Even if n doesn't go into p, and results in a decimal where the 10th place is less than 5, I want to force round up, how can I do this?

For example, 7/3 should return 3.

JDog1999
  • 15
  • 1

1 Answers1

0

Are you looking for something like this:

#include <stdio.h>

#define ARRAY_SIZE(array) \
    (sizeof(array) / sizeof(array[0]))

int main(void) {
    int n_list[] = { 7, 8, 9, 10, 11, 12 };
    int n = 7;
    int p = 3;
    int result;
    
    for (size_t index = 0; index < ARRAY_SIZE(n_list); index++) {
        n = n_list[index];
        result = n / p;
        if ((p * result) < n) {
            result++;
        }
        printf("result = %d\n", result);
    }

    return 0;
}

Output

result = 3
result = 3
result = 3
result = 4
result = 4
result = 4

Try it on repl.it.