2️⃣C Program to check Prime Number(With Program and Explanation)
C Program to Check Prime Number
๐น Introduction
A prime number is a natural number greater than 1 that has exactly two distinct positive divisors: 1 and itself. In this post, we will write a C program to check whether a given number is prime or not.
This program is useful for beginners to understand loops, conditions, and basic number logic in C.
๐น Problem Statement
Write a C program to check whether a given number is a prime number or not.
๐น Algorithm
-
Start
-
Read an integer
n -
If
n ≤ 1, it is not prime -
Check divisibility from
2ton/2 -
If divisible, number is not prime
-
Otherwise, number is prime
-
Stop
๐น Source Code (C Program)
๐น Output
Example 1:
Example 2:
๐น Explanation
-
The program first checks if the number is less than or equal to 1
-
It then checks divisibility from
2ton/2 -
If the number is divisible by any value in this range, it is not prime
-
Otherwise, it is a prime number
๐น Time Complexity (Beginners will learn this concept later on)
-
O(n) (can be optimized to √n)
๐น Optimized Version (Optional – Extra Value for Readers)
# (Beginners will learn this concept later on)
๐น Conclusion
This C program efficiently checks whether a number is prime. Beginners should first understand the basic version and then move to the optimized approach using sqrt(n).
๐ C Programming Series – What’s Next?
๐ In the next post of this series, we will learn how to write a C Program to Check Armstrong Number, with complete explanation and output examples. Stay tuned! ๐
Comments
Post a Comment