Need help with homework

Ok so my teacher is really bad at explaining things and then he gives us homework that is completely irrelevant I looked everywhere on google and could not find anything so I decided to make an account.

I need to display every 5th number from 0 to 50 using a loop but I have no idea please help? Thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
int main()
{
    for (unsigned short int n{0}; n <= 50; n++)
    {
        // if the remainder of n divided by 5 is 0 then n is one of the 5th numbers
        if ((n % 5) == 0)
        {
            std::cout << n << std::endl;
        }
    }
    return 0;
}
Last edited on
Actually, 0 % 5 = 0, but it is the first and not the fifth number. I don't know if your teacher will care about that though. If you don't want to output 0 as a result, change it to n{1}.

Similarly, does the range 0 to 50 include the number 50 or not? If you don't want to output 50 as a result, change it to n < 50.
A simpler solution would be something like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

int main()
{
    char response;
    
    for (uint16_t fifths = 5U; fifths < 50U; fifths += 5U) {
        std::cout << fifths << '\n';
    }

    std::cin >> response;
    return 0;
}


Also, if the number 50 is included in the loop then just change the operator in the condition of the for loop to <= instead of <.
Last edited on
Topic archived. No new replies allowed.