Skip to main content

Program to check whether a number is prime or not in C

Before we check whether a number is prime or not, we should know the answer for the question "What is a prime number ?"
Definition : A number which is only divisible by 1 and itself is a prime number. For example, numbers such as 1, 2, 3, 5, 7, 11 etc. are not divisible by any numbers other than 1 and themselves. So, they are all prime numbers. But, the numbers such as 4, 6, 8, 16 etc., are divisible by 2 and hence they are non-prime numbers. The number 9 is divisible by 3 and hence it is also no-prime number. Now, let us see " How to check whether a given number is prime or not?"

Procedure : Consider the number 16. It is not divisible by 9, 10, 11, 12, 13, 14 and 15 which are all greater than 16/2. So, the first point to remember is:
  • A number m cannot be divisible by a number which is greater than m / 2.
In our case m is 16. It is divisible by the numbers 2, 4 and 8 which are less than or equal to 16/2.
The second point to remember is:
  • A number m may be only divisible by either 2 or 3 or 4 or 5 ..... or m/2.
So, to check whether m is prime or not, it is sufficient to divide m by the numbers 2, 3, 4....m/2 one after the other. During this process, if the remainder is zero, the number m is non-prime.
But, even after dividing m by all the numbers from 2 to m/2, if the remainder is still not zero, the number m is prime.
The partial code is as follows 
for (i=2; i<=m/2; i++)

if(m%i == 0)
printf("The number is non-prime");

printf("The number is prime\n");

The algorithm and the equivalent flowchart are shown below:
Algorithm : PRIME
[This algorithm checks whether the given number is prime or not.]

Step 1: [Enter the number]
Read: n
Step 2: [Check for prime number]
for i=2 to n/2 in step 1
if(n%i == 0)
Write : 'Number is non-prime'
[End of if]
[End of for]
Step 3: [Output prime]
Write: 'number is prime'
Step 4: [finished]

The complete program to check whether the given number is prime or not is shown below:
Example : Program to check whether a number is prime or not.

main( )
int n, i;
printf("Enter a number:\n");
for(i=2; i<=n/2; i++)
if(n%i == 0)
printf("%d is non-prime",n);
printf("%d is prime",n);


Enter a number
i=2 3 4 5 6
13/2  13/3  13/4  13/5 13/6
2,3,4,5,6 can't divide 13
13 is prime


Enter a number: 
i=2 3 4
9/2  9/3

9 is non-prime


Popular posts from this blog

difference between structure and union in C Language

In c language article we will see the difference between union and structure. Both are the user define datatype in c language. See the table which is mentioned below: ASP.NET Video Tutorial Series Structure Union1.The keywordstruct is used to define a structure 1. The keyword union is used to define a union. 2. When a variable is associated with a structure, the compiler allocates the memory for each member. The size of structure is greater than or equal to the sum ofsizes of its members. The smaller members may end with unused slack bytes. 2. When a variable is associated with a union, thecompiler allocates thememory by considering the size of the largest memory. So, size of union is equal to the size of largest member. 3. Each member within a structure is assigned unique storage area of location. 3. Memory allocated is shared by individual members of union. 4. The address of each member will be in ascending order This indicates that memory for each member will start at different offset v…

Difference between Linear search and Binary Search in c language

SQL Video Channel : Download all SQL Video

Binary Search Linear Search Works only on sorted items. such as  1,2,3,4,5,6  etc
Works on sorted as well as unsorted items. 12,4,5,3,2,1 etc Very efficient if the items are sorted Very efficient if the items are less and present in the beginning of the list. such as Suppose your list items are : 12,3,4,5,1 and you want to search 12 number then you get beginning in the list. Works well with arrays and not on linked lists. Works with arrays and linked lists.
Number of comparisons are less More number of comparisons are required if the items are present in the later part of the array or its elements are more.

Memory representation of Linked List Data Structures in C Language

Memory representation of Linked List

             In memory the linked list is stored in scattered cells (locations).The memory for each node is allocated dynamically means as and when required. So the Linked List can increase as per the user wish and the size is not fixed, it can vary.

               Suppose first node of linked list is allocated with an address 1008. Its graphical representation looks like the figure shown below:

      Suppose next node is allocated at an address 506, so the list becomes,

  Suppose next node is allocated with an address with an address 10,s the list become,

The other way to represent the linked list is as shown below:

 In the above representation the data stored in the linked list is “INDIA”, the information part of each node contains one character. The external pointer root points to first node’s address 1005. The link part of the node containing information I contains 1007, the address of next node. The last node …