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 ....
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.
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;
}
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;
}