Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

/*Assignment 7: Write a program to print the prime numbers from 1 to a given number.

*/
START

Enter the last term of the series n

Initialize i =2

Is i<=n ? yes Initialize s=0 , j=2

no

no i=i+1

Is s==0 ? yes Print prime number i

no

Is j<=sqrt(i) ?

yes

Is i%j=0 ? yes S=1

no J=j+1

STOP

#include<stdio.h> #include<conio.h> #include<math.h> void main() { int i,j,n,k=0; clrscr(); printf("Enter the last term of the series\n"); scanf("%d",&n); printf("The prime numbers are:\n"); for(i=2;i<=n;i++) { k=0; for(j=2;j<=sqrt(i);j++) { if(i%j==0) k=1; } if(k==0) printf("%d\t",i); } getch(); } //printing condition of prime numbers //checking the condition of the prime number //starting loop of divisor for 1 to square root of the taken number //starting loop for each natural number up to the given number except 1

OUTPUT:
Enter the last term of the series 30 The prime numbers are: 2 3 5 7 11 13 17 19 23 29

You might also like