Monday 14 March 2016

Series 3


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;
}

5 comments:

  1. Hello sir, Thank you. But can you please explain the reason for calculating the square root and its comparison.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. can u please explain this code

    ReplyDelete