Help with returning.
EDIT: Oh...didn't read the question. Gimme a minmute.
So from this little bit of code, if you are setting belowThis to 10, are you trying to return 3, 5, 10?
What do you want the program to return?
Your OR that is an issue. It's double horizontal bars, not one.
if ( i % 3 == 0 || i % 5 == 0)
You sum all the values that makes the if statement true.
numbers = number + i;
Return the total value once it has finished with everything inside the for loop.
return numbers;
Make sure you declare your static int number. Otherwise, you could get crazy numbers depending on what was stored the block of memory in RAM when it was previously used for by the OS / another application.
static int numbers = 0;
If you were using your variable static int numbers like
numbers = i;
Then you don't NEED to initialize your variable as you'll be storing a new value into it anyways. Over-writing if you will.
You are NOT including 10, so your for loop should be this
for ( int i = 1; i < num; i++)
So...for belowThis = 10
static int numbers = 0;/* Other parts of your code Other parts of your code Other parts of your code*/for (int i = 1; i < num; i++){ if(i % 3 == 0 || i % 5 == 0) { numbers = numbers + i; }}return numbers;
When i is 1, if statement is FALSE, doesn't run what is inside the if
When i is 2, if statement is FALSE, doesn't run what is inside the if
When i is 3, if statement is TRUE, numbers = 0 + 3
When i is 4, if statement is FALSE, doesn't run what is inside the if
When i is 5, if statement is TRUE, numbers = 3 + 5
When i is 6, if statement is TRUE, numbers = 8 + 6
When i is 7, if statement is FALSE, doesn't run what is inside the if
When i is 8, if statement is FALSE, doesn't run what is inside the if
When i is 9, if statement is TRUE, numbers = 14 + 9
total is 23
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now