Wednesday, October 3, 2018

CPL 7 - PRIME NUMBER

7. Implement using functions to check whether the given number is prime and display appropriate messages. (No built-in math function)

ALGORITHM:

Step 1: Start 

Step 2: [Read the value of N(positive integers)]
    Read N 

Step 3: [to check whether N is prime or not]
For i=2 to m/2 
If m%i=0 then go to step 4 otherwise go to step 5 

Step 4: Print message  not a prime number

Step 5: Print a message  prime number 

Step 6: stop


FLOWCHART:( click on image to zoom )



PROGRAM:

#include<stdio.h>
int isprime(int n);

int main()
{
int i,m1,m2;
printf("enter range\n");
scanf("%d%d",&m1,&m2);
printf("prime num from %d to %d are:\n",m1,m2);
for(i=m1;i<=m2;i++)
{
if(isprime(i))
printf("%d\n",i);
}
}

int isprime(int m)
{
int i;
for(i=2;i<=m/2;i++)
{
if(m%i==0)
{
return 0;
}
}
return 1;
}


OUTPUT:( click on image to zoom )



1 comment:

  1. The objective of the program is to check whether given number is prime number or not. But your program finds the prime numbers present between the entered range.

    ReplyDelete