Skip to content

Commit

Permalink
primevalue.c
Browse files Browse the repository at this point in the history
  • Loading branch information
dhanu-r authored Oct 2, 2023
1 parent 6f193fd commit f3dc191
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions Add Code Here/C/primevalue.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <stdio.h>
int checkPrimeNumber(int n);
int main() {

int n1, n2, i, flag;

printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);

// swap n1 and n2 if n1 > n2
if (n1 > n2) {
n1 = n1 + n2;
n2 = n1 - n2;
n1 = n1 - n2;
}

printf("Prime numbers between %d and %d are: ", n1, n2);
for (i = n1 + 1; i < n2; ++i) {

// flag will be equal to 1 if i is prime
flag = checkPrimeNumber(i);

if (flag == 1) {
printf("%d ", i);
}
}

return 0;
}

// user-defined function to check prime number
int checkPrimeNumber(int n) {
int j, flag = 1;

for (j = 2; j <= n / 2; ++j) {

if (n % j == 0) {
flag = 0;
break;
}
}

return flag;
}

0 comments on commit f3dc191

Please sign in to comment.