% operator

Pages: 123
GRex, you're getting remainders mixed up with decimal fractions.

The remainder of 137/50 is 37.

50 goes into 137 twice, with 37 left over. So the answer is 2 r37.
That's what I'm saying. I was saying how 2 * 50 + 74 = 174, so the other code couldn't be right. I'm saying that 100 / 33 = 3 R1, 33 * 3 + 1 = 100
Apologies, misread your post. Thought that was the answer you were coming up with. :-p
Not a problem.
137/50 = 2.74. Quotient = 2 Decimal # = 74. I called it the remainder.Maybe that's why I'm wrong.

Modulus of 137/50 = 37.

I tried to show how to get the decimal after the decimal #.

Integer calculation:

137 / 50 = 2.
100 * 137 = 13700
13700 / 50 = 274;
2 * 100 = 200;
274 - 200 = 74.

result 2.74.
This is used a lot in microcontrollers instead of floating point.

What is the question? Find modulus or find the reminder of a division?
Last edited on
Find the quotient of division with a remainder. 137 / 50 = 2 R37.
Last edited on
Here is a program where you can select how many decimal after the decimal point you want.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>

using namespace std;

int Quotient(long a, long b);
int Remainder(long a, long b, int c);

// function quotient
int Quotient(long a, long b)
{
    return a / b;
}

// Function remainder
int Remainder(long a, long b, int c)
{
int quotient;
int remainder;
int mul;

    quotient = a / b;

    if (c == 1){
        a*= 10; // 1 decimal after the decimal point
        mul = 10;
    } else if (c == 2) {
        a*= 100; // 2 decimal after the decimal point
        mul = 100;
    } else if (c == 3) {
        a*= 1000; // 3 decimal after the decimal point
        mul = 1000;
    } else {
        a*= 10; // default. 1 decimal after the decimal point
        mul = 10;
    }
    remainder = a / b;
    remainder = (remainder - (quotient * mul));
    return remainder;
}


int main()
{
 int a;
 int b;
 int c;
 int r;

    cout << "Enter a divisor! \n";
    cin >> a;
    cout << "Enter a dividend! \n";
    cin >> b;
    cout << "Enter decimals! 1 to 3 \n";
    cin >> c;
    cout << "The quotient of those numbers is " << Quotient(a, b) << endl;
    cout << "The remainder of those numbers is " << Remainder(a, b, c) << endl;

    r = Remainder(a, b, c);

    if (c == 1) {
        cout << "The number is " << Quotient(a, b) << "." << r << endl;
    } else if (c == 2) {
        if (r > 9) {
            cout << "The number is " << Quotient(a, b) << "." << r << endl;
        } else {
            cout << "The number is " << Quotient(a, b) << ".0" << r << endl;
        }
    } else if (c == 3) {
        if (r > 99) {
            cout << "The number is " << Quotient(a, b) << "." << r << endl;
        } else if (r > 9) {
            cout << "The number is " << Quotient(a, b) << ".0" << r << endl;
        } else {
            cout << "The number is " << Quotient(a, b) << ".00" << r << endl;
        }
    }
    cin >> a;
    return 0;
}
Simplified.

1
2
3
#include <iomanip>

cout << setiosflags(ios::fixed) << setprecision(number);  //number is number of decimal places. 
So far the biggest problem you're having is settling on an algorithm to find the remainder.

Implement division in terms of subtraction:

Given a value n divided by a value d the following is true:

the number of times you can subtract d from n and still have n >= d is the quotient.

the remainder is the value of n on the first iteration where n<d

Should be pretty straightforward to implement.
Modulus of 137/50 = 37.

I tried to show how to get the decimal after the decimal #.

Integer calculation:

137 / 50 = 2.
100 * 137 = 13700
13700 / 50 = 274;
2 * 100 = 200;
274 - 200 = 74.

result 2.74.

Remainder: (74 * 50) / 100 = 37.

Am I correct? This is a straight forward calculation without using % (modulus). 2r37.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Function remainder
int Remainder(long a, long b, int c)
{
int quotient;
int remainder;

    quotient = a / b;

    a*= 100; // 2 decimal after the decimal point

    remainder = a / b;

    remainder = (remainder - (quotient * 100)); // the number after the decimal point

    reminder = (reminder * b) / 100; // The reminder

    return remainder;
}


The simple way of modulo without %.


1
2
3
4
5
6
7
8
// Function remainder
int Remainder(int a, int b)
{
int quotient;

    quotient = a / b;
    return (a - (quotient * b));
}
Last edited on
Is anyone going to pay attention to me practically posting this on the last page? I feel Perman and I are the only ones with varying ideas and nobody's actually looked at mine. Sure, it means that a loop is involved, but it definitely works, after you put it in a function with two address of variables.
GRex2595, I like your code on the last page. My problem is I have very little knowledge of C++ so I am not sure how to compile you code. I am still trying to learn the basics, I have been watching tutorials on thenewboston.com and have been reading a C++ book.

You want a void function with 4 variables.

void funName(int, int, int &, int &);

Those ampersands (&) mean that you are passing the variable, not just the value. This means that you are able to change these variables within the function.

When I gave you the code, you had almost all the info you needed. You just needed a function to perform the work.

1
2
3
4
5
6
7
8
9
10
11
12
void funName(int a, int b, int &quot, int & remainder)
{

int total = 0;

for(int c = 0; b * c <= a; c++)  //yes 0, what if it's 0 / 27
  {
    total = b * c;
    quot = c;
  }
remainder = a - total;
}


You want to put the first piece of code before int main() in your program. You want to put the second piece of code after int main(). When you call to it, you are going to a = dividend, b = divisor, quot = quotient, remainder = remainder.

You will call it within main like this.

 
void funName(divid, divis, quotient, remain);


All the variable names as well as the function name are optional. Just make sure that you get the variable type right.
GRex2595:

I think we got the problem solved after some misunderstanding. There are several ways to do it. I came up with 2 and you came up with one and they all give the same result. Ha Ha. Had to read the whole post from the start to figure it out. One last comment. You wrote "Those ampersands (&) mean that you are passing the variable, not just the value." I think you mean the address of the variable and not the variable itself. Enjoy. Lets help the kids. They are our future.
Per Hojfeldt.
Last edited on
I'm very confused with where this topic went. The modulo/remainder operator can be done much simpler than any of the methods in this post.
I'm very confused with where this topic went. The modulo/remainder operator can be done much simpler than any of the methods in this post.


This.
Gaminic.

Show me how to make it simpler without use of % than:

a = 137
b = 50

c = a / b // 2
a = a - ( b * c) // 37

2r37

c = quotient // 2
a = remainder // 37
Last edited on
That is the method I was hinting to in my first post; hadn't even found it in your posts until I went back for it. Still, two pages for it to appear?
Ha Ha. I misunderstood what was mend by remainder. To simple. When it gets simple it get complex. The only times I have been using the modulo (%) have been when rounding numbers in microcontrollers. What about yourself?
The Danish Viking.
This is where I am. I think I really close to how this program needs to run.


#include <iostream>
#include <cstdlib>
using namespace std;

    int remainder(int a, int b){
    int quotient;
    quotient = a / b;
    return (a - (quotient * b));
}

int main (int a, int b){
    cout << "Enter a divisor! and press Enter \n";
    cin >> a;
    cout << "Enter a dividend! and press Enter \n";
    cin >> b;
    cout << "The remainder of those numbers is " << remainder << endl;
    system("PAUSE");
    return 0;
}
Pages: 123