#14 Prime Number Checker in C: A Program with Sample Outputs

Prime numbers are an intriguing mathematical concept that plays a significant role in various fields, including cryptography and number theory. Determining whether a given number is prime or not is a common problem in programming. In this blog post, we will explore a C program that checks whether a number is prime and provide sample outputs for better understanding.

Code Explanation

  1. The program starts by including the necessary header files 'stdio. h' and 'math. h'. These headers provide the required functions for input/output operations ('printf', 'scanf') and mathematical operations ('sqrt').
  2. The 'main()' function is the entry point of the program.
  3. It declares the required variables: 'n' (the number to be checked), 'i' (used as a loop counter), and 'flag' (to determine if the number is prime or not).
  4. The program prompts the user to enter a number by displaying the message "Enter a number: " using the 'printf()' function.
  5. The 'scanf()' function is used to read the user's input and store it in the variable 'n'.
  6. Next, there is an 'if' condition to check if the entered number 'n' is equal to 1. If it is, it means the number is not prime because prime numbers are defined as numbers greater than 1. In this case, the program prints "1 is not a prime number." and then returns 0 to terminate the program.
  7. If the entered number is not 1, the program proceeds to the 'for' loop. It iterates from 'i = 2' to the square root of 'n'. This optimization is used because factors of a number always occur in pairs, and the larger factor is always equal to or less than the square root of the number.
  8. Inside the loop, it checks if the number n is divisible evenly by 'i' (i.e., if 'n % i == 0'). If it is, it means the number is not prime.
  9. If the number is found to be divisible by any 'i', the flag variable is set to 1, indicating that the number is not prime, and the loop is exited using the 'break' statement.
  10. After the loop, the program checks the value of the 'flag' variable. If it is still 0, it means the number is prime because it was not divisible by any number other than 1 and itself.
  11. Finally, the program uses 'printf()' to display the appropriate message based on the value of the 'flag' variable. If the 'flag' is 0, it prints that the number is prime. Otherwise, it prints that the number is not prime.
  12. The 'return 0' statement at the end of the 'main()' function indicates successful program execution, and the program terminates.

The blog post will feature the full C program code to enhance reader understanding. By including the complete code, readers can follow along, and deepen their knowledge of the topic concisely.



Sample Output:
Let's see a few examples of the program's output:






Conclusion
In this blog post, we have explained the code for a C program that checks whether a given number is prime or not. By using a loop and checking for divisibility from 2 up to the square root of the number, the program effectively determines whether the number is prime. The code provides a valuable tool to understand the concept of prime numbers and can be used as a starting point for further exploration of number theory.

Post a Comment

0 Comments