Help with the question

Hi, this is my first time learning programming and i am just trying out some practice questions but having some trouble with this question.Thank You. Your help will be very much appreciated.


Write a program that prints all of the numbers less than 2000 that are evenly divisible by 10. The first few programs are 10 20 30 ....


Show your attempt and explain where you are stuck...
use a for/while loop.
Use a for-loop and make it run between 0 and 2000. Then ask if "i" or whatever you call the index inside the for-loop leaves 0 as remainder after you've divided it by 10, if so, print it out. Becuase only numbers that are evenly divided by 10 has a remainder of 0.
Last edited on
for(int i=0; i<=2000; i+=10) cout<< i <<endl;
@anup30

while that does print things out correctly. It is not the best way of doing it. That code is hard coded. This is better

1
2
3
4
5
6
for (int i = 1; i <= 2000; i++) 
	{
		if (i % 10 == 0) // Divides i by 10 and checks if it leaves 0 as remainder.
						// Only numbers that are evenly divided by 10 has 0 as remainder.
			cout << i << endl;
	}
Last edited on
hard coded ??

how is it hard coded if it uses less cpu time ?

@tarikneaj..we haven't done if statement in class so far so is it possible to do it just using for loop?

@Anup30

I might have gotten the term wrong (hard coded) Sorry about that. I think I meant more like, yours just directly does it, while the code I provided actually checks everything and calculates.

@tesla2015

Yes. If you want to do it without using if statements. Then you do it like @Anup30 Suggested.

1
2
3
4
for (int i = 10; i <= 2000; i+=10) // i = 10 so it doesnt print out 0.
	{
		cout << i << endl;
	}
Last edited on
there is mention of lower bound in the problem. i.e -50 is evenly dividable by 10.(result =-5; remainder =0;)
@anup30 I dont understand what you're trying to say?

Yes, -50 divided by 10 leaves 0 as a remainder. But he only wanted positive numbers.
Topic archived. No new replies allowed.