Home > Software engineering >  How to make a series that adds up by 5 every 5 counts using for loop?
How to make a series that adds up by 5 every 5 counts using for loop?

Time:02-19

I'm kind of new in C programming and I'm trying to make a program that prints the nth term of a series and every 5 counts, it adds up by 5.

Example: 1, 2, 3, 4, 9, 10, 11, 12, 17, 18, 19, 20, 25......

Here is my code

int num,digit,count = 1;

printf("Enter n: ");
scanf("%d", &num);

for(int i=1; i<=num; i  ){
    count  ;
    if(count > 5){
        count = 0;
        i =4;
    }
    
    printf("%d ",i);

}

My code doesn't get to the specific nth term that I'm asking for. For example, I've inputted 10 and it only shows up until the 6th term

CodePudding user response:

The thing to do is get clear in your head what you want to do and how you are going to do it. Rather than tinkering with code, break things down into simple parts and make them clear.

#include <stdio.h>


int main(void)
{ 
    int num = 10;

    //  Method 1:  Spell everything out.

    //  i counts number of terms printed.
    //  j counts number of terms since last multiple of four terms.
    //  k is current term.
    for (
        int i = 0, j = 0, k = 1;    //  Initialize all counters.
        i < num;                    //  Continue until num terms are printed.
          i)                        //  Update count.
    {
        printf("%d ", k);           //  Print current term.
          j;                        //  Increment four-beat count.
        if (4 <= j)
        {
            //  Every fourth term, reset j and increment the term by 5.
            j = 0;
            k  = 5;
        }
        else
            //  Otherwise, increment the term by 1.
            k  = 1;
    }
    printf("\n");

    //  Method 2:  Use one counter and calculate term.

    //  Iterate i to print num terms.
    for (int i = 0; i < num;   i)
        /*  Break the loop count into two parts:  the number of groups of 4
            (i/4) and a subcount within each group (i%4).  Looking at the
            starts of each group (1, 9, 17, 25...), we see each is eight
            greater than the previous one.  So we multiply the group number by
            8 to get the right offsets for them.  Within each group, the term
            increments by 1, so we use i%4 directly (effectively multiplied by
            1).  Then (i/4)*8   i%4 would start us at 0 for i=0, but we want to
            start at 1, so we add 1.
        */
        printf("%d ", (i/4)*8   i%4   1);
    printf("\n");
}

CodePudding user response:

Replacing your loop with this one, it will work as well:

for(int i=0, j=1, count=1; i<num; i  , j  , count  )
{
    printf("%d ", j);
    if(count == 4)
        count = 0, j =4;
}

CodePudding user response:

You shall not change the variable i within the body of the for loop.

You need to introduce one more variable that will store the current outputted number.

Here is a demonstration program.

#include <stdio.h>

int main(void) 
{
    unsigned int n = 0;
    
    printf( "Enter n: " );
    
    scanf( "%u", &n );
    
    for ( unsigned int i = 0, value = 0, count = 1; i < n; i   )
    {
        if ( count == 5 )
        {
            value  = 5;
            count = 1;
        }
        else
        {
              value;
        }
        
        printf( "%u ", value );
        
          count;
    }
}

The program output is

Enter n: 13
1 2 3 4 9 10 11 12 17 18 19 20 25

CodePudding user response:

Here's another take that I think is a bit less complicated, with explanations in the comments:

#include <stdio.h>

int main(void)
{
    int num, count = 1;

    num = 20;
    // if you look closely, you actually have an initial condition before the
    // main pattern starts. Once the pattern starts, its actually every _fourth_
    // term that gets added by 5.  You'll make things easier on yourself if you
    // print out this initial condition, then handle the pattern in the loop.
    // If you really want to be correct, you can wrap this in a if (num > 0) check
    printf("%d ", count  );
    // start at 1, because we already printed the first item
    for(int i=1; i<num; i  , count  )
    {
        // now we can focus on every fourth term
        if (i % 4 == 0)
        {
            // if it's the fourth one, add 5 to the previous value
            // Of course this simplifies to count  = 4
            count = (count-1)   5;
        }
        
        printf("%d ",count);
    }
}

Demonstration

  • Related