Perl Program to Check Whether a Number is Prime or Not

0
2473

If you are searching for prime number program in Perl, here ends your search. Before going to the program part, it is vital to understand a prime number.It is a natural number which is larger than 1 but has no positive divisors other than that number and 1. But a number which is not prime and greater than 1 is called as “composite” number.

Between 1 to 100, there are 25 prime numbers.  They are: 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61, 67,71,73,79,83,89,97  Perl program to find prime numbers

PROBLEM DEFINITION

Write a program to check whether a number is prime or not.

ALGORITHM

Step 1: Start

Step 2: Read the number to n.

Step 3: Initialize d (flag) to 0.

Step 4: if n is equal to 2,print number is prime.

Step 5:  check if n%c=0,set flag=1

Step 6: if flag=1,break from loop n print not prime , else print prime.

Step 7: Stop

Perl program to find prime numbers

print “Enter a numbern”;

\$n=<>;

\$d=0;

if(\$n==2)

{

print “Prime number.n”;

}

else

{

for(\$c=2;\$c<=\$n-1;\$c++)

{

if(\$n%\$c==0)

{

\$d=1;

break;

}

}

if(\$d==1)

{

print “Not prime.n”;

}

else

{

print “Prime number.n”;

}

}

OUTPUT

Enter a number

5

Prime Number

CONCLUSION

The program was executed successfully. 