45. Series 3

Image result for c programming

Write a program to generate the first n terms in the series  2,3,5,7,11,...,17

Input Format:
Input consists of a single integer which corresponds to n.

Output Format:
Output consists of the terms in the series separated by a blank space.

Sample Input:
8

Sample Output:
 2 3 5 7 11 13 17 19

Code:
  #include<stdio.h>
#include<math.h>
int main(){
  int n,div,count,num,t;
  scanf("%d",&n);
  printf("%d ",2);
  count=1;
  num=3;
  while(count<n)
  {
    t=sqrt(num);
    div=2;
      while(div<=t)
      {
        if(num%div==0)
          break;
        div++;
      }
    if(div>t)
    {
      printf("%d ",num);
      count++;
    }
    num=num+2;
  }
  return 0;
}

No comments