# Thread: Division using repeated subtraction

1. ## Division using repeated subtraction

Okay so I am suppose to write a program that divides two numbers without using the operator "/"
So i try writing the program using repeated subtraction, but I dont know if I am doing this right at all.

Heres my code
Code:
```#include <stdio.h>

int main()
{
int a, b, max, min, results, count;
max = 2000000000;
min = -2000000000;
results = 0;
count = 0;

printf("Enter integer numbers 'a' and 'b' for division a/b: ");
scanf("%d %d", &a, &b);

if (b == 0) {
printf("Division by zero is not allowed!");
printf("\n");
return 0;
}
while (1) {
results = a - b;
count++;
if (b < a)
break;
}
printf("Division result is %d\n", count);
return 0;```

2. B is never going to be less than A unless it is to start with, because you aren't actually changing B or A. You're doing:

C = A - B
D++

And then are checking to see if B is less than A, without ever actually changing anything other than C and D.

Quzah.

3. okay so i fixed it up but for some reason its not printing.

Code:
```#include <stdio.h>

int main()
{
int a, b, max, min, results, count;
max = 2000000000;
min = -2000000000;
results = 0;
count = 0;

printf("Enter integer numbers 'a' and 'b' for division a/b: ");
scanf("%d %d", &a, &b);

if (b == 0) {
printf("Division by zero is not allowed!");
printf("\n");
return 0;
}
while (1) {
results = a - b;
count++;
if (result == 0) {
count++;
break;
}
}
printf("Division result is %d\n", count);
return 0;```

4. Code:
```        while (1) {
results = a - b;
count++;
if (result == 0) {
count++;
break;
}
}```
You are setting results to "a - b" in each iteration, and a and b are never changed.

It's not printing because your program is stuck in an infinite loop.

5. Code:
```while ("he doesn't get it")
{
printf("The values of 'a' and 'b' are not changing in the loop, so neither will 'result'!!!\n");
}```

6. ok check out this code.. i made it for you i hope it helps...

Code:
```#include <stdio.h>
#include <stdlib.h>
int main()
{

int a;
int b;
int i;
char cha[10];
char chb[10];
printf("Enter number: ");
gets(cha);
a = atoi(cha);
printf("Enter divider: ");
gets(chb);
b= atoi(chb);
printf("\n\n%d/%d is %d and the remainder is %d\n\n",a,b,a/b,a%b);
while(a>=b){
i = a - b;
printf("%d - %d = %d\n",a,b,i);
a=i;
}

return 0;
}```
cheers... tropche.

7. I'm pretty sure the OP has worked out a solution by now.

8. anyways, this is not a problem only for him, it might be useful to someone else who is trying to acomplish the same thing... so i think i did right to bring a possible solution to this problem... at least have some respect for me as a new member....

thank you

9. It has nothing to do with respect - it's just a matter of relevance. Responding to old threads just creates unnecessary discontinuity (as it isn't really a conversation anymore, at that point). Besides that, the OP's question was effectively answered, so there really isn't any point in posting a "solution", anyway (explanations are better advice than code solutions, by the way, as they promote "learning").

Oh, and welcome to the board.

10. Originally Posted by tropche
i think i did right to bring a possible solution to this problem.
no you did not

at least have some respect for me as a new member....
ಠ_ಠ

11. Originally Posted by tropche
anyways, this is not a problem only for him, it might be useful to someone else who is trying to acomplish the same thing... so i think i did right to bring a possible solution to this problem...
I dig this. After I joined this place I used to submit code solutions all the time just to learn and get my mind working. There's no harm done. And I find solutions to things every day by googling and rummaging thru posts that are several years old on all kinds of programming forums. I've got a dozen computer forums bookmarked right now where I'm member. I've got questions about everything and answers for half of that.

But I don't blame you for calling attention to the fact, Sebastiani. And I'm not actually gonna look a the code either because I have more important jokes to tell than reading thru an old thread with a math problem for a title.

Anyway tropche, you have the right attitude about leaving stuff for posterity, esp. if you figure it is a common enough interest.