Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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");
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading