A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. The first few prime numbers are 2, 3, 5, 7, 11, and so on. To check if a number is prime or not, we can use the concept of loop structures in C programming.
In this article, we will explore a C program to check if a number is prime.
Understanding the Logic
The logic of the program is quite straightforward. Here is the procedure to follow:
- Take an input number from the user.
- Check if the number is less than or equal to 1. If it is, it’s not a prime number. If not, proceed to the next step.
- Loop through 2 to the square root of the given number:
- If the number is divisible by any number in this range, it’s not a prime number.
- If it’s not divisible by any number in this range, it’s a prime number.
The reason we check divisibility up to the square root of the number is because a larger factor of the number would be a multiple of a smaller factor that has already been checked.
Code Implementation in C
Now let’s write the C code based on the logic we described:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include<stdio.h> #include<math.h> int main() { int num, i, flag = 0; printf("Enter a positive integer: "); scanf("%d", &num); for(i = 2; i <= sqrt(num); i++) { if(num%i == 0) { flag = 1; break; } } if (num <= 1) { flag = 1; } if(flag == 0) printf("%d is a prime number.\n", num); else printf("%d is not a prime number.\n", num); return 0; } |
Code Explanation
Here’s a detailed breakdown of the code above:
- #include<stdio.h> and #include<math.h>: These are preprocessor directives which tell the C compiler to include the stdio.h and math.h files before going to the actual compilation.
- int main(): The execution of the C program begins from the main() function.
- int num, i, flag = 0;: Here, we are declaring three integer variables num (to store the number entered by the user), i (to use in the loop), and flag (to check whether the number is prime or not).
- printf(“Enter a positive integer: “);: This function is used to print the message inside the double quotes.
- scanf(“%d”, &num);: This function is used to take input from the user. Here, %d is a format specifier. &num is the address of the variable num.
- for(i = 2; i <= sqrt(num); i++): This loop will run from 2 to the square root of the given number.
- if(num%i == 0): Checks if num is divisible by i. If it is, flag is set to 1 and the loop is broken.
- if (num <= 1) {flag = 1;}: Checks if the number is less than or equal to 1, in which case it is not prime.
- The last if…else structure is used to print whether the number is prime or not based on the value of flag.
Program Execution
If we execute this program and enter 29 when prompted for a positive integer, we would get the following output:
1 2 | Enter a positive integer: 29 29 is a prime number. |
On the other hand, if we enter 4, we would get:
1 2 | Enter a positive integer: 4 4 is not a prime number. |
This C program accurately checks whether an entered number is prime. If you’re learning C or need to check for prime numbers in your code, this program offers a straightforward and effective way to do it.