Guest User

Untitled

a guest
Nov 22nd, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3.  
  4.  
  5. int is_prime(int n) {
  6. // We know that 2 and 3 are primes. So if we get
  7. // 2 or 3 then directly return 1 (true)
  8. if((n == 2) || (n == 3))
  9. return 1;
  10.  
  11. for(int i=2; i<=n/2; ++i) {
  12. // If i divides n any time, then the number is not prime, return 0 (false)
  13. if(n%i == 0)
  14. reutrn 0;
  15. }
  16.  
  17. // If we reach here that means the number is either prime or 1.
  18. // Since 1 is not prime, check for it and return accordingly
  19. return n!=1;
  20. }
  21.  
  22.  
  23. void main() {
  24. clrscr();
  25. for(int i=1; i<=20; ++i) {
  26. if(is_prime(i))
  27. printf("%d\n", i);
  28. }
  29. getch();
  30. }
Add Comment
Please, Sign In to add comment