C Program & Algorithm to check whether the given number is Prime or not
This is the C program code and algorithm for checking whether the given number is prime or not.
Aim:
Write a C program to check whether the given number is prime or not.
Algorithm to check whether the given number is Prime or not
Algorithm:
Step 1: Start
Step 2: Read number n
Step 3: Set f=0
Step 4: For i=2 to n-1
Step 5: If n mod 1=0 then
Step 6: Set f=1 and break
Step 7: Loop
Step 8: If f=0 then
print 'The given number is prime'
else
print 'The given number is not prime'
Step 9: Stop
Program code
#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr();
int n,i,f=0;
printf("Enter the number: ");
scanf("%d",&n);
for(i=2;i<n;i++)
{
if(n%i==0)
{
f=1;
break;
}
}
if(f==0)
printf("The given number is prime");
else
printf("The given number is not prime");
getch();
}
Output
Enter the number : 5 The given number is prime