Program to Check Prime Number in C
A number that can only be divisible by Itself or 1. You can see that it's clear a number is divisible by itself or one then use a loop starting from one like condition=1 and max condition up to <=Number and divide a number by condition and increase the counter by ++ if the number divided after the end of the count is equal to 2 then the number is prime otherwise not.
#include <stdio.h>
int check_prime(int num);
int main()
{
int n1, n2, i, flag;
printf("Enter two numbers(intervals): ");
scanf("%d %d", &n1, &n2);
printf("Prime numbers between %d and %d are: ", n1, n2);
for (i = n1 + 1; i < n2; ++i)
{
flag = check_prime(i);
if (flag == 0)
printf("%d ", i);
}
return 0;
}
int check_prime(int num) /*User-defined function to check prime number*/
{
int j, flag = 0;
for (j = 2; j <= num / 2; ++j)
{
if (num % j == 0)
{
flag = 1;
break;
}
}
return flag;
}
Condition for Prime Number in C
A number that can only be divisible by Itself or 1. You can see that it's clear a number is divisible by itself or one then use a loop starting from one like condition=1 and max condition up to <=Number and divide a number by condition and increase the counter by ++ if the number divided after the end of the count is equal to 2 then the number is prime otherwise not.
0 Comments: