# Not sure why this failed

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 08-06-2012
garth
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; /* Your Logic goes here */ //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.
• 08-06-2012
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.
• 08-06-2012
whiteflags
It's possible you misunderstood the problem statement. From your example numbers:
Quote:

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.
• 08-06-2012
garth
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.
• 08-06-2012
stahta01
What answers does you code give?

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

Tim S.
• 08-06-2012
garth
Quote:

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;           /*         Your Logic goes here        */     cout<<"Total is:";   }```
• 08-06-2012
Matticus
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.
• 08-06-2012
ZuK
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
• 08-06-2012
whiteflags
I did some sleuthing. If this is the source, then the answer is supposed to be in Java.
• 08-06-2012
Matticus
Quote:

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.
• 08-06-2012
ZuK
Quote:

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
• 08-06-2012
garth
Quote:

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
• 08-06-2012
oogabooga
Quote:

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;`
• 08-06-2012
whiteflags
Quote:

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.
• 08-06-2012
garth
Quote:

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.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last