Promi_38

Prime numbers upto N

Dec 6th, 2020
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.40 KB | None | 0 0
  1. /* Write a code to print the prime numbers within a given of 1 to n.
  2. Input:
  3. 10
  4. Output:
  5. 2
  6. 3
  7. 5
  8. 7 */
  9.  
  10. #include<stdio.h>
  11.  
  12. int prime(int n)
  13. {
  14.     int i;
  15.     if(n != 2 && n % 2 == 0) return 0;
  16.     for(i = 3; i*i <= n; i++)
  17.     {
  18.         if(n % i == 0) return 0;
  19.     }
  20.     return 1;
  21. }
  22.  
  23. int main()
  24. {
  25.     int n, i;
  26.     scanf("%d", &n);
  27.    
  28.     for(i = 2; i <= n; i++)
  29.     {
  30.         if(prime(i))
  31.         {
  32.             printf("%d\n", i);
  33.         }
  34.     }
  35. }
  36.  
Advertisement
Add Comment
Please, Sign In to add comment