# Arithmetic Issue

So, I was trying to make some simple code that would start A at ten, start B at one, and every loop, increase B by one, and add that to A, and the result goes in C, so it would be like this:
11, 13, 16, 21, 26, etc
But the code only adds 1 to A and prints it.
Code:

``````int a = 10;
int b = 1;
int c = 0;
const int delaySpeed = 1000; // Speed of output. Default one second
void increaseAmount() {
b = b + 1;
c = a + b;
}
void setup() {
Serial.begin(9600);
}
void loop() {
delay(delaySpeed);
increaseAmount();
Serial.println(c);
}
``````

Thanks for help!

``````  c = a + b;
``````

try

``````c += (a + b);
``````

It's doing exactly what you're telling it to - which is something different from what you want.

Do it out by hand (this is what you should always do when you run into an issue like this):

a=10
b=1
c=0

First round:
b=b+1 (1+1=2 - b is now 2)
c=a+b (10+2=12 - c is now 12)

Second round:
b=b+1 (2+1=3 - b is now 3)
c=a+b (10+3 = 13 - c is now 13)

and so on.

BulldogLowell:

``````  c = a + b;
``````

try

``````c += (a + b);
``````

Sorry, That didn't work.

DrAzzy:
It's doing exactly what you're telling it to - which is something different from what you want.

Do it out by hand (this is what you should always do when you run into an issue like this):

a=10
b=1
c=0

First round:
b=b+1 (1+1=2 - b is now 2)
c=a+b (10+2=12 - c is now 12)

Second round:
b=b+1 (2+1=3 - b is now 3)
c=a+b (10+3 = 13 - c is now 13)

and so on.

I forgot to mention
B does not increase, It stays at 1
so the output is this:
10
11
12
13
14
15
16
17
18
19
20
etc

B does not increase

How do you know this?

so the output is this:
10
11
12
13
14
15
16
17
18
19
20

That's what happens when you increment by one.

No. The point is that you never change the variable a. So b goes 2, 3, 4 etc but you keep adding that to 10.

What you need to do after incrementing b is add it to the previous RESULT not to a which is still 10.

Steve