prime number program in c

In this post, we will see about prime number program in c.
The prime number is the number which is divisible only by two numbers, 1 and itself.

Let’s write simple c program.

Output:

Enter a positive integer to check prime number: 19
19 is a prime number.

If you notice here, we have for loop up to num/2.If loop iterates upto num/2 and flag is equal to 0 then number is prime number else number is not prime number.
Actually, you can simply check up to square root of number and it will work fine.You don’t need to check upto num/2.
Let’s change the program and loop upto square root of num.

Output:

Enter a positive integer to check prime number: 23
23 is a prime number.

That’s all about prime number program in C.

Leave a Reply

Your email address will not be published. Required fields are marked *