Click here to Skip to main content
16,012,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi guys i want to sum all the multiples of 3 between 1 and 100
this is what i tried
C#
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
	int t;
	for (int i=1 ;i<=100; i++)
	{
	cout << i+3<< " ";
	}
	
		
	return 0;
} 
Posted
Comments
CHill60 16-Dec-15 5:33am    
And what is the problem?
Jochen Arndt 16-Dec-15 5:38am    
It looks like homework so I will not give you a working solution.

I interpret "multiple of 3" as multiplying 3 with the loop count (you are printing i+3).

To sum up the values just add the above to a variable. You already declared 't'. So that may be used. But you should initialise it with zero.

Your title says "using while loop" but you actually have a for loop. So you might want to change the loop.

[EDIT]
It seems I misunderstood the requirement.

Multiples of three are: 3, 6, 9, ... .
These should be summed up for values between 1 and 100 using a while loop.

Think about the above two statements and the solution should come to your mind.
Member 12173667 17-Dec-15 0:10am    
thanks don't give me the code keep to your selve
it's not a homework by the way
but i got an answer
thanks any way MR(won't give code)

There are a number of ways. Here are a few:

1. add the numbers 3, 6, 9, ... to 99.
2. add the numbers 1 to 33 and multiply by 3.
3. 3 * (33*33 + 33)/2
4. 1683

I've added this solution as a compliment to Griff's solution. If you pick #3 or #4, make sure you understand the math involved and are prepared to explain your answer to the professor.
 
Share this answer
 
I'd do something simpler - but since this is your homework, I won;t give you the code!
Instead of that, use a for loop that runs with i between 1 and 100/3. Declare a variable called "sum" outside the loop, and set it to zero.
Then inside the loop, add the value "i * 3" to "sum" each time.
After the loop, print the value of "sum".
 
Share this answer
 
Comments
Animesh Datta 17-Dec-15 1:40am    
very good explanation .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900