This is a very simple approach to the problem; using loops and basic [+-]operators.
If you need an answer in decimals, you could use a times_ten and divide_by_ten function. In that case, you should take a look at the atoi() function;
times_ten would extract the integer in a char-array, adding a '0' in the end before converting it back to integer.
divide_by_ten would store the last character of an integer, substract this character with a '.' and adding the stored last digit back to the array before converting it back to integer. Atoi() will round the integer based on the decimal which we manipulated in the char-array.
Heres a version only supporting integer results, with an extra function (leftover_division()) replacing the '%'-operator.
[b]Passing pointers to integers instead of regular integers to the divide_rounded() function and adjusting the value of 'a' in divide_rounded() should make the leftover function redundant, saving a lot of calculation time if you'd need to know the lefover.[/b]
#include <stdio.h>
#include <stdlib.h>
int divide_rounded(int a, int b){
int outcome_rounded = 0;
while(a > b){
a = a - b;
outcome_rounded ++;
}
return outcome_rounded;
}
int leftover_division(int a, int b){
while (a >= b){
a = a - b;
}
return a;//this will return remainder
}
main(){
int number = 20;
int divisor = 3;
int outcome;
int leftover;
outcome = divide_rounded(number, divisor);
leftover = leftover_division(number, divisor);
printf("[%d] divided by [%d] = [%d] + [%d]\n", number, divisor, outcome, leftover);
}