Division using repeated subtraction

This is a discussion on Division using repeated subtraction within the C Programming forums, part of the General Programming Boards category; Okay so I am suppose to write a program that divides two numbers without using the operator "/" So i ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    23

    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. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    23
    okay so i fixed it up but for some reason its not printing.
    please help,

    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. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    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. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,293
    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");
    }
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    2

    Wink

    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. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    I'm pretty sure the OP has worked out a solution by now.

    Don't revive dead threads.
    Code:
    #include <ip.hpp>

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    2
    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. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    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.
    Code:
    #include <ip.hpp>

  10. #10
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by tropche View Post
    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. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tropche View Post
    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.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Get jiffies?
    By pgzh in forum Linux Programming
    Replies: 6
    Last Post: 03-17-2008, 10:30 AM
  2. Binary division of large numbers
    By hooper in forum C Programming
    Replies: 5
    Last Post: 06-01-2007, 02:33 AM
  3. Division By Subtraction
    By blacksnake in forum C++ Programming
    Replies: 19
    Last Post: 01-13-2007, 09:17 AM
  4. Check for repeated digits!
    By CrackerJack in forum C Programming
    Replies: 5
    Last Post: 11-08-2003, 10:37 PM
  5. Modular Division problem
    By Malek in forum C++ Programming
    Replies: 7
    Last Post: 05-24-2003, 06:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21