possible continuation? pls help

read 2 numbers (ex: 4 7) increment 2 and decrement 1 so it will be like these:
4 6 5 7 6 8 7. (4 plus 2 = 6 -1 = 5...)
im stuck at this point, i end up getting 14 numbers(the double) instead of seven because of the two cout´s in the loop, so i wanted to know a possible continuation for this problem.

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

using namespace std;

int main()
{
    int inic,sec;
    cin>>inic;
    cin>>sec;
    for(int i=1;i<=sec;i++){

        inic=inic+2;
        inic --;
        cout<<inic<<" ";
    }

    return 0;



thankyou
Last edited on
You're doing both steps at once instead of printing each separate, on lines 12 and 13.

And since you want it to only print 7 (sec) numbers, you'll need to either change the for loop, or remember if you're on an odd vs. even iteration.

Possible solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;

int main()
{
    int inic,sec;
    cin >> inic;
    cin >> sec;
    for (int i = 0; i < sec; i++)
    {
        cout << inic << " ";
        
        // alternate every iteration
        if (i % 2 == 0)
            inic = inic + 2;
        else
            inic--;
    }

    return 0;
}

If I weren't busy I'd make a more creative solution, but this will do.
thanks!
Topic archived. No new replies allowed.