For while loop

how would I write a for loop to print all the multiples of 5 from 25 up to and including the value of the integer variable limit.
and how do i make it exit the loop if the value is greater than 100?
Last edited on
Use the modulus operator % to check in a loop whether the value is dividable by the range 5-25. Set the limit to i <= 100;

1
2
3
4
5
6
7
8
9
10
for (int i=0; i <= 100; ++i)
{
    for (int u = 5; u < 25; ++u)
    {
        if (i % u == 0)
        {
            std::cout << i << "\n";
        }
    }
}
I tried to break up your post inside the comments to the program below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

int main()
{
    int limit = 150;
    for(            //how would I write a for loop
        int i = 25; //from 25
        i <= limit; //up to and including the value of the integer variable limit
        i += 5      //all the multiples of 5
       )
    {
        //to print
        std::cout << i << std::endl;

        if (i > 100) //if the value is greater than 100
        {
            //make it exit the loop
            break;
        }
    }

    return 0;
}


EDIT: You would normally write all the for-loop stuff before that for loop's opening brace '{' on the same line, but I broke it up so that I could append a comment to each part.
Last edited on
Why do you need the if statement? It means that the loop will exit before the limit. So why not just set the limit to 100?
The limit is unspecified in the question. I assumed that the limit may be variable, and it could be greater than, less than, or equal to 100. I think this question is meant to get the OP to create a for loop and to invoke a statement to alter control flow within a loop based on some condition.
Topic archived. No new replies allowed.