Shishu

write a program to print all the prime numbers from 1 to n

Nov 16th, 2017
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.59 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3.  
  4. int main()
  5. {
  6.  
  7.     int N, i, j, isPrime, n;
  8.  
  9.     printf("To print all prime numbers between 1 to N\n");
  10.     printf("Enter the value of N\n");
  11.     scanf("%d",&N);
  12.  
  13.  
  14.     printf("Prime numbers between %d to %d\n", 1, N);
  15.  
  16.     for(i = 2; i <= N; i++)
  17.     {
  18.         isPrime = 0;
  19.  
  20.         for(j = 2; j <= i/2; j++)
  21.         {
  22.  
  23.             if(i % j == 0)
  24.             {
  25.                 isPrime = 1;
  26.                 break;
  27.             }
  28.         }
  29.  
  30.         if(isPrime==0 && N!= 1)
  31.             printf("%d ",i);
  32.     }
  33.     getch();
  34.     return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment