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?
p.s. not sure why string included. I didn't need it.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; }