Write a program in C to check whether a number is a prime number or not using the functions.
#include<stdio.h>
#include<stdbool.h>
bool isPrime(int);
int main()
{
int n; /*’n’ is user defined number.*/
printf(“Enter a number :”);
scanf(“%d”,&n);
if(isPrime(n)) /*if isPrime() return’s TRUE, then if statement will execute otherwise else will execute*/
printf(“Given number is PRIME number\n”);
else
printf(“Given number is Non-PRIME number\n”);
return 1;
}
bool isPrime(int n)
{
int i,count;
count=0;
for(i=1;i<=n;i++)
{
if(n%i==0) /*check the iteration value is factor of given number or not.*/
count++;
}
if(count==2)
return 1; /*true & false are defined words from “stdbool.h” we can also use 1 & 0*/
return 0; /*Here we did not use “else” because when a “return” statement appears Compiler automatically terminates the function and returns the value, so when the if condition fails then only “return 0” will execute*/
}
Output:
i) Enter a number : 7919
Given number is PRIME number
ii)Enter a number : 852
Given number is Non-PRIME number
Other Prime number programs
Write a C Program to Find Whether the Given Number is Prime Number or not.
Write a C program to generate first ‘n’ prime numbers, where n is a value Supplied by the user.