Home > Back-end >  How to print Even Number after 4 Odd Number with C
How to print Even Number after 4 Odd Number with C

Time:12-09

I use this script to print odd number from 1 to 30.

#include <iostream>

using namespace std;

int  main( )   {
    int  i  =  1 ;
    
    // whele loop from 1 to 30
    while (i  <= 30)  {
        cout << i<< " , ";
        i = i   2;
    }
    
    return 0;
    
}

So the output Will be 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29

What i must to do if i want to add Even number after 4 Odd numbers. The output Will be like 1, 3, 5, 7, 10, 11, 13, 15, 17, 20, 21, 23, 25, 27, 30.

I hope someone can help me with this problem. Thank you

CodePudding user response:

At each iteration you print a number as you are doing now, however, every 5th iterations what you do is a bit different. This is a condition you need to test in each iteration: if true, print like this, if false, print like that.

Start with the loop as you did. Since you using a counting variable that you always increment, a for loop seems more natural than a while.

int main() {
    for(int i = 1; i <= 30; i  = 2) {
        cout << i << " , ";
    }

    return 0;
}

Now you add the condition test. For that, you can introduce another variable (numOddNumbers) to keep track of how many odd numbers you have printed.

int main() {
    int numOddNumbers = 0;
    for(int i = 1; i <= 30; i  = 2) {
        if(numOddNumbers > 0 && numOddNumbers % 4 == 0) {
            cout << i   1 << " , ";
            numOddNumbers = 0;
        } else {
            cout << i << " , ";
            numOddNumbers  ;
        }
    }

    return 0;
}

For the condition we can use numOddNumbers % 4 == 0 to check that numOddNumbers is amultiple of 4, but since we do not want the condition to be true when numOddNumbers == 0 we also make sure that numOddNumbers > 0.

Notice that differently from i, the numOddNumbers variable is only incremented when the condition is false, since that is when we print an odd number.

This code will print

1 , 3 , 5 , 7 , 10 , 11 , 13 , 15 , 17 , 20 , 21 , 23 , 25 , 27 , 30 ,

Note: An interesting exercise for you is to simplify this code to use just the counting variable, without the extra numOddNumbers variables. The pattern is well defined and you know that the difference is in every 5th itertion.

CodePudding user response:

I can offer the solution of your problem.

#include <iostream>
#include <vector>
using namespace std;
void showContentVector(vector<int>& input)
{
    for(int i=0; i<input.size();   i)
    {
        cout<<input[i]<<", ";
    }
    return;
}
void oddEvenNumbers(int start, int stop, int quantity, vector<int>& input)
{
    if(start>stop)
    {
        return;
    }
    if(start%2!=0)
    {
        input.push_back(start);
          quantity;
    }
    if(quantity==4)
    {
        start =3;
        input.push_back(start);
        quantity=0;
    }
    oddEvenNumbers(start 1, stop, quantity, input);
    return;
}
void solve()
{
    vector<int> inputVector;
    oddEvenNumbers(0, 30, 0, inputVector);
    cout<<"inputVector <- ";
    showContentVector(inputVector);
    return;
}
int main()
{
    solve();
    return 0;
}

Here is the result:

inputVector <- 1, 3, 5, 7, 10, 11, 13, 15, 17, 20, 21, 23, 25, 27, 30, 
  •  Tags:  
  • c
  • Related