Computer Science, asked by shabadineelambika, 10 months ago

c program to generate the following series where N which is upper limit should be taken as an input. -1,0,2,5,10,17,28,41,58,81....N

Answers

Answered by pradeeprengaswamy
0

Answer:

#include<stdio.h>

int main ()

{

 int N, i = 3, count, c, prev;

 printf ("Enter the upper limit\n");

 scanf ("%d", &N);

 if (N <= 0)

   {

     printf ("Enter a positive number");

     return 0;

   }

 if (N > 0)

   printf ("-1, ");

 if (N > 1)

   printf ("0, ");

 if (N > 2)

   printf ("2, ");

 prev = 2;

 for (count = 4; count <= N;)

   {

     for (c = 2; c <= i - 1; c++)

{

  if (i % c == 0)

    break;

}

     if (c == i)

{

  if (count == N)

    printf ("%d ", i + prev);

  else

    printf ("%d, ", i + prev);

  prev = prev + i;

  count++;

}

     i++;

   }

 return 0;

}

Explanation:

This program takes an upper limit and provides the required series. We generate prime numbers to the required series limit and add them to the previous numbers in series.

Similar questions