2

compsci novice here. I am supposed to program 2 user inputted values and have them multiplied and divided without using the * and / operators.

I have figured out how to multiply but not divide...

    //Multiplication
    cin >> digit1;
    cin >> digit2;

    float product = 0;

    for (int i = 0; i < digit2; i++) product = product + digit1;
    cout << product << endl;

    return 0;

as for division I'm not exactly sure...

    cin >> digit1;
    cin >> digit2;

    float quotient = 0;

    //Enter Division operation
    cout << quotient << endl;
    return 0;

Thanks for the help!

Moojave
  • 37
  • 2
  • 7

2 Answers2

3

For divide, one could do something like this (below would be digit2 / digit1):

int movingNum = 0;
int i = 0;
while(movingNum < digit2){
    // This is the counter of how many times digit1 goes into digit2, increment it
    // because the running sum is still less than digit2
    i++;

    // increment the moving sum by digit1 so that it is larger by digit1 moving through
    // the next iteration of the loop
    movingNum += digit1;
}

cout << "digit1 goes into digit2 " << i << " times.";

For digit1 / digit2:

int movingNum = 0;
int i = 0;
while(movingNum < digit1){
    // This is the counter of how many times digit2 goes into digit1, increment it
    // because the running sum is still less than digit1
    i++;

    // increment the moving sum by digit2 so that it is larger by digit2 moving through
    // the next iteration of the loop
    movingNum += digit2;
}

cout << "digit2 goes into digit1 " << i << " times.";

These are obviously for integer divide, if the two inputs do not divide equally, then there will be a remainder. This remainder can be calculated after the above loop by:

int remainder = movingNum - digit2;

If you are truly looking for a floating point answer / result, this would be an entirely different answer.

trumpetlicks
  • 7,033
  • 2
  • 19
  • 33
  • It does not work for me, I keep getting the output as 0 – Moojave Feb 20 '14 at 04:57
  • @Moojave is digit1 greater than digit2??? If it is than the answer SHOULD be 0. Is the requirement to have digit1 / digit2, or digit2 / digit1??? Multiplying is easy, dividing requires an understanding of order! – trumpetlicks Feb 20 '14 at 04:59
  • I was hoping to lead OP to this without stating it although I was thinking of using subtraction in my loop and looping while > 0. Using the increasing sum you get an easy way to compute the remainder though. – pstrjds Feb 20 '14 at 05:04
  • I made a mistake actually, I don't need the % modulus at all. My bad, I just need to divide 2 numbers where I cannot use / – Moojave Feb 20 '14 at 05:07
  • @pstrjds - I like moving forward in the loop because it allows for the (what looks to be unsigned divide) actual result to be ~2x the capable result of a true int. I didn't want to confuse the OP with unsigned int versus regular int however. If indeed his numerics are all positive, than moving forward will allow him to find results that are on the order of 2x the size of a true int versus an unsigned. – trumpetlicks Feb 20 '14 at 05:07
  • @Moojave - Have you tried the 2nd code portion of my answer? – trumpetlicks Feb 20 '14 at 05:15
  • @trumpetlicks yes, I inputted 40 and 8, and I got the output as 4 instead of 5... – Moojave Feb 20 '14 at 05:16
  • @Moojave - I will edit, I think that movingNum needs to be initialized to 0 instead of digit2! – trumpetlicks Feb 20 '14 at 05:32
3

Division is repeated subtraction.

Example: 4 / 2 literally means, how many 2s constitute 4 (which is, quotient). So, subtract the dividend by divisor until dividend becomes zero while keeping a count of the number of times you're subtracting divisor(here, 2) from dividend(here, 4).

Try this code (This is not for floating point answers. If that is what you want, let me know! :D).

#include<stdio.h>

int main() {

    int divi, divs, quo = 0, rem = 0;

    scanf("%d %d", &divi, &divs);

    for(rem = divi - divs; rem >= 0; rem = rem-divs) {

        if(rem < 0)                         // to ensure that the right answer is obtained when the divisor isn't a factor of dividend.
          break;
        else
          quo++;
  }

printf("%d\n", quo);

return 0;

}