# Thread: Not sure why this failed

1. ## Not sure why this failed

Write a program which calculates the sum of natural numbers which are divisible by 3 and 5 less than the number given as input.

for ex.
1) If user input 10 then output will be 23.
2) If user input 20 then output will be 78.

so i figure 10 = 3,6,9,5
20 = 3,6,9,12,15,18,5,10 (no duplicate 15)

I failed. 0/3 Response: Your logic is incorrect !

I fail to see the problem in my logic. Can you?

Code:
```#include <iostream>
#include <string>

using namespace std;

int main(int argc, char* argv[])

{
int max = 0, sum = 0;
cout << "Enter the maximum No.:";
cin >> max;

/*
*/

//three
int three = max/3;
// remove itself i.e 10 and 20 from the equation
if (max%3 == 0)
three--;

for (int x=three;x>0;x--)
{
sum = sum + (3 * x);
}

//five
int five = max/5;
if (max%5 == 0)
five--;

for (int x=five;x>0;x--)
{
// only add to sum if not a duplicate (already present from three)
// couldn't get the not equals to work so went with true
if ((5*x)%3 == 0)
{

}
else { sum = sum + (5 * x); }

}

cout<<"Total is:";
cout << sum;

return 0;
}```
p.s. not sure why string included. I didn't need it.

2. If you're submitting it to an automated testing system then you can't just print out anything you want. Maybe you need to remove the "Enter..." prompt and the "Total is" string.

3. It's possible you misunderstood the problem statement. From your example numbers:
20 = 3,6,9,12,15,18,5,10 (no duplicate 15)
What in the problem statement says that the sum can't include 2 fifteens or any other number? If you missed some terms in the sum and provided a wrong answer, well then, you have your explanation.

4. I don't think so. At least not in that respect. I have probably put you wrong by using the terminology "duplicate". It only wants the natural numbers. so you don't want to repeat a natural number. At least that's how I figure they got 78.

5. What answers does you code give?

Is there a logical reason you are multiplying by 3 or 5?

Tim S.

6. Originally Posted by oogabooga
If you're submitting it to an automated testing system then you can't just print out anything you want. Maybe you need to remove the "Enter..." prompt and the "Total is" string.
I'm hoping its something like that. Although this is the code they provide you with. I've tried returning sum, or omitting any output. no joy

Code:
```#include <iostream>
#include <string>

using namespace std;

int main(int argc, char* argv[])
{
int max=0,sum=0;

cout<<"Enter the maximum No.:";
cin>>max;

/*
*/

cout<<"Total is:";

}```

7. If the examples you provided were given to you, then I suspect the wording in the problem is wrong.

If we assume that it said "sum of natural numbers which are divisible by 3 or 5 less than the number given as input," this would be very easy to solve and would match the given examples.

Try writing down the numbers 1 through 9. Cross off any that aren't divisible by either 3 or 5, and add up the numbers you have left. Do the same for numbers 1 - 19. I did this myself and got the results you provided in the given examples.

8. If you do it this way
Code:
```int test( int max ) {
int sum = 0;
for ( int x = 3; x < max; ++x ) {
if ( 0 == x % 3 )
sum += x;
else if (  0 == x % 5 )
sum += x;
}
return sum;
}```
It gives the exact same results then your code.
If the results are wrong then it must be a missunderstanding of the problem
Kurt

9. I did some sleuthing. If this is the source, then the answer is supposed to be in Java.

10. Originally Posted by ZuK
If you do it this way...
That's almost how I did it, except I logic "or"ed the two conditions to make it more concise. I believe, however, that you just handed the solution to the assignment to the OP.

11. Originally Posted by Matticus
I believe, however, that you just handed the solution to the assignment to the OP.
Did I ?. The op already had a solution that gives the same results.
Kurt

12. Originally Posted by whiteflags
I did some sleuthing. If this is the source, then the answer is supposed to be in Java.
that is the site and the question but you can navigate to the cpp side of things. code challenges -> cpp -> easy

13. Originally Posted by garth
I'm hoping its something like that. Although this is the code they provide you with. I've tried returning sum, or omitting any output. no joy
Okay. So that stuff needs to be there exactly like that.
What about the lack of a newline after printing sum
Code:
`cout << sum;`
Try
Code:
`cout << sum << endl;`

14. Originally Posted by garth
that is the site and the question but you can navigate to the cpp side of things. code challenges -> cpp -> easy
I did it the way you said and I still end up at the Java challenge.

15. Originally Posted by whiteflags
I did it the way you said and I still end up at the Java challenge.
yeah I had the same problem. you need to wait for the page to fully load. then change to cpp. then wait for it to fully load. refresh latency issue on the site.