write an algorithm to check a number is prime number or not
Answers
Answer:
Aim:
Write a C program 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
Explanation:
hope it helps you
An algorithm to check a number is a prime number or not:
- Step 1: Start
- Step 2: Taking initialization of N, temp=1, i=2
- Step 3: Read N from the user
- Step 4: If num<=1 // Numbers which are less than 1 are not considered as the prime number.
Display "N is not a prime number"
Go to step 7
- Step 5: Repeat the steps until i<[(c/2)+1]
If the remainder of the number divided by j equals 0,
Set temp=0
Go to step 6
i=i+1
- Step 6: If temp==0,
Display N+" is not a prime number"
Else
Display N+" n is a prime number"
- Step 7: End