# Perl Program to Check Whether a Number is Prime or Not

0
3289

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.

SHARE
Previous articlePHP Programming Basics-String Operations
Jesna is an assistant professor, digital marketing strategist and professional blogger who stumbled upon writing online since 2009. She started her blogging career in 2010, and never looked back. Since then, she has developed an incredible passion for writing about all sorts of tech. Away from blogosphere, she loves to paint and is the founder of fineartblogger.com