Thursday, August 16, 2012

Prime Number

Assalamu Alaikum. Hope, you all are well by the grace of Almighty ALLAH. Today we learn how to write a  C programming code of a prime number. take a look at the problem.

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



Solution:
A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself.

Take a look at the following program.


In this program we check the number N from 2 to (N-1). We can also use line no 17 and 18 instead of line 13-16.

We can also do this program by the following way.


We can also do this program by checking the number N from 2 to N/2. Take a look at the program.


We can also do this program by checking the number N from 2 to square-root(N). Take a look at the program.


We can also do this program by using a flag. take a look at the program.


Thank You all..




2 comments: