To generate series 1, 3, 6, 11, 18, 29, 42? in C Language it is important to understand the logic behind it. If we observe the difference between each number is like 2,3,7,11. So these are prime numbers. To generate such a series it is important that the difference between numbers should be an incremental prime number. So below program will generate a series up to user input by having prime number difference.
// Online C Program to generate Series up to Number
// 1, 3, 6, 11, 18, 29, 42?
#include <stdio.h>
int main() {
//variable declaration
int i,j,num,primeFlag,seriesNum,Scount;
printf("\n how many numbers you need in this Series 1, 3, 6, 11, 18, 29, 42? to Print\n");
scanf("%d",&num);
//for loop to print numbers
seriesNum =1; //This variable to store number from series.
Scount=1; //variable to check count of numbers in series.
printf("\t%d",seriesNum);
do
{
primeFlag=0; //flag to check if number is prime or not.
i= i+1;
//loop to find prime number and add it to previous
for(j=1;j<=i;j++)
{
if((i%j)==0)
{
primeFlag++;
}
else
{
//continue
}
}
if (primeFlag == 2)
{
//here if number is divisible by 1 and self number then only primeFlag will become 2
seriesNum = seriesNum + i;
printf(" %d,",seriesNum);
Scount = Scount + 1;
}
}while (Scount < num);
return 0;
}
how many numbers you need in this Series 1, 3, 6, 11, 18, 29, 42? to Print
22
1 3, 6, 11, 18, 29, 42, 59, 78, 101, 130, 161, 198, 239, 282, 329, 382, 441, 502, 569, 640, 713,