1. C Programming for Beginners: Writing Your First Hello World Program in C in 2023 2. Mastering Datatypes in C Programming: A Comprehensive Guide for Beginners in 2023 3. Complete Guide to % Format Specifiers in C for Beginners in 2023 4. A Comprehensive Guide to Operators in C Programming: Learn Everything About Operators in C in 2023 5. Master C Operator Precedence and Associativity in 2023: Definition, Examples, and Code 6. Learn Increment and Decrement Operators in C: A Beginners Guide with Examples - Codzify 7. Mastering Bitwise Operators in C: A Comprehensive Guide with Examples - Codzify 8. Ternary Operators in C Programming: A Beginners Guide (2023) 9. Switch Statements in C 2023: Complete Guide for Beginners 10. Learn Functions in C Programming - A Complete Tutorial for Beginners in 2023 11. Call by Value and Call by Reference in C Programming - A Complete Tutorial for Beginners in 2023 12. Storage Classes in C Programming: A Comprehensive Guide for 2023 13. Arrays in C Programming: A Comprehensive Guide for 2023 14. Basics of 2D Array in C Programming in 2023: Definition, Initialization, and Accessing Elements 15. Beginners Guide to Strings in C Programming with Codes and Examples 16. Strings Functions in C Language - strlen, strcpy,strcmp,strcat functions 17. A Comprehensive Guide to Structures in C Programming in 2023 18. Learn Unions in C Programming with Codes, Examples, and Explanations in 2023 19. Dynamic Memory Allocation in C: Understanding malloc, calloc, realloc, and free Functions 20. Introduction to C Programming 21. Hello world program in C 22. String functions in C - strncat, strcpy, strncpy, strchr, strrchr, strstr functions 23. Program for Fibonacci number in C - Codzify.com 24. What is palindrome number in C? 25. Write a C program to find a factorial of a given number 26. Write a C program to check whether a given number is prime or not 27. Write a C program to check whether a given number is an Armstrong number or not. 28. Write a C Program to transpose a matrix 29. C Programming Tutorial: Understanding Comments and Identifiers with Code Examples 30. Master String Handling Functions in C in 2023 - Codzify Topics

Write a C program to check whether a given number is prime or not

Article by: Manish Methani

Last Updated: September 27, 2021 at 8:04am IST
2 min 13 sec

prime number is a number greater than 1 with only two factors – themselves and 1. 

A prime number cannot be divided by any other numbers without leaving a remainder.

An example of a prime number is 13. It can only be divided by 1 and 13. Dividing a prime number by another number results in numbers left over e.g. 13 ÷ 6 = 2 remainder 1.

15 is not an example of a prime number because it can be divided by 5 and 3 as well as by itself and 1.

C program to check whether a given number is prime or not.

#include 
int main()
{
    int number = 7;
    char flag = 0;
    for(int i = 2; i <= (number/2); i++)
    {
      if(number % i == 0)
      {
         flag = 1;
         break;
      }
    }

if (number == 1)
{
   printf("1 is neither prime nor composite.");
}
else
{
  if(flag == 0)
     printf("
%d is a prime number.", number);
  else
     printf("
%d is not a prime number.", number);
}

return 0;
}

Output

7 is a prime number.

In this program, a loop is iterated from 2 to number/2. In each iteration, using the condition (number % i) it is checked whether a given number is perfectly divisible by i. 

If a number is perfectly divisible by i, then the flag is set to 1 and break. After iteration, we will check whether a given number is 1 or not. if it is 1, the number is considered as a non-prime number else, if the flag is 0, then the given number is a prime number else the number is not a prime number.

Explore Tech Guide:

Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer

Contact: teamcodzify@gmail.com