program using addition only

This is a discussion on program using addition only within the C Programming forums, part of the General Programming Boards category; I am trying to make a calculator program using addition only...I have made the code for multiplication, but am having ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    10

    program using addition only

    I am trying to make a calculator program using addition only...I have made the code for multiplication, but am having trouble with - and division.
    I created division with just subtraction but can't figure it out with addition.

    heres my one example:
    insert
    Code:
    int divide, divis, quotient;
    printf("Enter Number 1:");
    scanf("%d", &divide);
    
    printf("Enter Number 2:");
    scanf("%d", &divis);
    quotient=0;
    while(divide > 0){
    divide = divide - divis;
    quotient++;
    }
    printf("%d\n, quotient);

  2. #2
    Registered User
    Join Date
    Apr 2011
    Posts
    14
    Use the Euclidean Division algorithm with your algorithm for multiplication if you're using integers. Here's a link to read up on it. The translation to code is pretty easy. If you're messing with decimal values, you're going to need something else, but this will solve your issue with integer division.

    Loads of info:
    Euclidean algorithm - Wikipedia, the free encyclopedia

    Specific info:
    Division and Euclidean algorithms

    More specific info:
    http://www.cargalmathbooks.com/10%20...gorithm%20.pdf

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    10
    yes i am using integers, so can I use that for both division and subtraction?

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    14
    Not exactly. It's not a solve-all algorithm, unfortunately, but you can use some operations on sets (in C, arrays) to do subtraction by addition. Read up a little on it, because it's set theory mathematics, but I imagine someone on this forum could come up with a better way of doing it than I.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. matrix addition program using arrays
    By suryak in forum C Programming
    Replies: 6
    Last Post: 04-10-2011, 07:37 AM
  2. Replies: 4
    Last Post: 10-08-2010, 02:30 PM
  3. Bug in the addition drill program
    By Sharifhs in forum C Programming
    Replies: 1
    Last Post: 08-13-2010, 11:50 PM
  4. Addition Program - Help
    By ggraz in forum C Programming
    Replies: 6
    Last Post: 10-15-2008, 10:45 PM

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