C Program To Check Prime Number By Creating a Function Last Updated : 20 Aug, 2024 Comments Improve Suggest changes Like Article Like Report Write a C program that checks whether a given number is a prime number by creating a dedicated function. A dedication function is better when we want to use the code multiple times in the program without making any changes.Example:Input: N = 7Output: PrimeExplanation: 7 has no divisors other than 1 and 7, so it is a prime number.Input: N = 10Output: Not PrimeExplanation: 10 is divisible by 1, 2, 5, and 10, so it is not a prime number.Different Methods to Check Prime Numbers Using Function in CTable of ContentUsing Function for Trial Division ApproachUsing Function for Optimized Trial Division Approach1. Using Function for Trial Division ApproachThe idea of this approach is to check the divisibility of the number N with each number from 2 to Nâ1. If it is completely divisible by any number, then it is not a prime number.Implementation C // C Program to Check Prime Number using Simple Trial // Division Approach #include <stdio.h> int isPrime(int N) { // Check divisibility from 2 to N-1 for (int i = 2; i < N; i++) { // If N is divisible by i, it is not a prime number if (N % i == 0) { return 0; } } // If no divisors were found, N is a prime number return 1; } int main() { int N = 10; printf("Is %d prime?\n", N); // Check if the number is prime if (isPrime(N)) { printf("Yes\n"); } else { printf("No\n"); } return 0; } OutputIs 10 prime? No Time Complexity: O(N)Auxiliary Space: O(1)2. Using Function for Optimized Trial Division ApproachIn the above approach, we only need to check for factors up to the square root of N because if N is divisible by any number greater than its square root, then the quotient would be smaller than the square root and would have been checked already.Implementation C // C Program to Check Prime Number using Square Root Optimized // Trial Division Approach #include <math.h> #include <stdio.h> int isPrime(int N) { // Check divisibility from 2 to sqrt(N) for (int i = 2; i <= sqrt(N); i++) { // If N is divisible by i, it is not a prime number if (N % i == 0) { return 0; } } // If no divisors were found, N is a prime number return 1; } int main() { int N = 10; printf("Is %d prime?\n", N); // Check if the number is prime if (isPrime(N)) { printf("Yes\n"); } else { printf("No\n"); } return 0; } OutputIs 10 prime? No Time Complexity: O(âN)Auxiliary Space: O(1)Related Articles:Prime Numbers in CProgram to find Prime Numbers Between given Interval Comment More infoAdvertise with us Next Article C Program To Check Prime Number By Creating a Function L laxmigangarajula03 Follow Improve Article Tags : C Programs C Language C Basic Programs Similar Reads Prime Number Program in C A prime number is a natural number greater than 1 and is completely divisible only by 1 and itself. In this article, we will learn how to check whether the given number is a prime number or not in C.Examples:Input: n = 29Output: 29 is PrimeExplanation: 29 has no divisors other than 1 and 29 itself. 4 min read C Program To Find Prime Numbers Between Given Range A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. For example, 2, 3, 5, 7, and 11 are prime numbers. In this article, we will learn how to find all the prime numbers between the given range.ExampleInput: l = 10, r = 30Output: 11 13 17 19Explanat 6 min read Program to find Prime Numbers Between given Interval Given two numbers m and n as interval range, the task is to find the prime numbers in between this interval.Examples: Input: m = 1, n = 10Output: 2 3 5 7Input : m = 10, n = 20Output : 11 13 17 19Table of Content[Naive Approach] Basic Trial Division Method - O(n*n) Time and O(1) Space[Optimised Appro 15+ min read C Program to Check Whether a Number Can Be Express as Sum of Two Prime Numbers Prime numbers are numbers that have only 2 factors, 1 and themselves. For example, 2, 3, 5, 7, 11, etc are some of the first prime numbers. Here we will see whether a number can be expressed as the sum of two prime numbers using a C program. Example Input: 7Output: YesExplanation: 7 can be expressed 2 min read C Program to Display Prime Numbers Between Two Intervals Using Functions Prime numbers have only 2 factors, 1 and themselves. For example, 2,3, 5, 7, 9,... are the first 5 prime numbers. Here we will build a C program to display prime numbers between two intervals using functions using 2 approaches, for loop and while loop. Example Input: num1 = 2, num2 = 10 Output: Prim 3 min read Like