Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <conio.h>
- int is_prime(int n) {
- // We know that 2 and 3 are primes. So if we get
- // 2 or 3 then directly return 1 (true)
- if((n == 2) || (n == 3))
- return 1;
- for(int i=2; i<=n/2; ++i) {
- // If i divides n any time, then the number is not prime, return 0 (false)
- if(n%i == 0)
- reutrn 0;
- }
- // If we reach here that means the number is either prime or 1.
- // Since 1 is not prime, check for it and return accordingly
- return n!=1;
- }
- void main() {
- clrscr();
- for(int i=1; i<=20; ++i) {
- if(is_prime(i))
- printf("%d\n", i);
- }
- getch();
- }
Add Comment
Please, Sign In to add comment