Mohsin Mohsin - 27 days ago 8
C++ Question

Skipping values in a sequence of positive integers

I want to print the following number sequence:

1 2 3 6 7 8 11 12 13 16 17 18...

It prints three positive integers then skips two following values and then repeats the process.

Solution:(answered by Matthew Watson)

For first 100 positive integers

for (int i = 1; i <= 100; i += ((i%5) == 3) ? 3 : 1)
{
cout<<i<<" ";
}

Answer

Here's a simple way just using a for loop statement:

for (int i = 1; i < 100; i += ((i%5) == 3) ? 3 : 1)
{
    // ...
}