program that randomly chooses between add and subtraction.

Write your question here.
I am trying to make a program that that randomly selects 2 numbers then either adds or subtracts them. I've learned how to make the randoms numbers but i can't figure out how to randomize the addition or subtraction part of the program.

for example the first time the programs run it outputs 5 + 10 = 15, then the next time its ran it outputs 100-50 = 50
1
2
3
4
5
6
7
8
9
10
int x = rand() % 2;
if(x == 0)
{    
    if(num1 > num2)
       total = num1 - num2;
    else
        total = num2 - num1;
}
else
    total = num1 + num2;


So you generate a random 1 or 0 using %(modulus) 2 and then that determines the arithmetic

hope this helps :)
Last edited on
make another random number generator in a range of 1 to 0 to determine whether the number should be added or subtracted.

if(random==1)
x=y+z;

else
x=y-z;

EDIT:nevermind
Last edited on
Topic archived. No new replies allowed.