prime number c program
Here is a C program to check if a number is prime:
#include <stdio.h>
int main() {
int num, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &num);
for (i = 2; i <= num / 2; ++i) {
if (num % i == 0) {
flag = 1;
break;
}
}
if (num == 1) {
printf("%d is not a prime number.", num);
}
else {
if (flag == 0)
printf("%d is a prime number.", num);
else
printf("%d is not a prime number.", num);
}
return 0;
}
This program takes a positive integer as input and checks if it is a prime number. It uses a for loop to iterate from 2 to num / 2
and checks if the number is divisible by any of the numbers in this range. If it is divisible by any number, the flag
variable is set to 1, indicating that the number is not prime. At the end, the program checks the value of flag
and prints whether the number is prime or not.