Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C
  • C Basics
  • C Data Types
  • C Operators
  • C Input and Output
  • C Control Flow
  • C Functions
  • C Arrays
  • C Strings
  • C Pointers
  • C Preprocessors
  • C File Handling
  • C Programs
  • C Cheatsheet
  • C Interview Questions
  • C MCQ
  • C++
Open In App
Next Article:
Program to find Prime Numbers Between given Interval
Next article icon

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 = 7
Output: Prime
Explanation: 7 has no divisors other than 1 and 7, so it is a prime number.

Input: N = 10
Output: Not Prime
Explanation: 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 C

Table of Content

  • Using Function for Trial Division Approach
  • Using Function for Optimized Trial Division Approach

1. Using Function for Trial Division Approach

The 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; } 

Output
Is 10 prime? No 

Time Complexity: O(N)
Auxiliary Space: O(1)

2. Using Function for Optimized Trial Division Approach

In 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; } 

Output
Is 10 prime? No 

Time Complexity: O(√N)
Auxiliary Space: O(1)

Related Articles:

  • Prime Numbers in C
  • Program to find Prime Numbers Between given Interval


Next Article
Program to find Prime Numbers Between given Interval

L

laxmigangarajula03
Improve
Article Tags :
  • C Language
  • C Programs
  • C Basic Programs

Similar Reads

  • C Program to Check for Odd or Even Number
    Write a C program to check whether the given number is an odd number or an even number. A number that is completely divisible by 2 is an even number and a number that is not completely divisible by 2 leaving a non-zero remainder is an odd number. Example Input: N = 4Output: EvenExplanation: 4 is div
    4 min read
  • C Program to check whether a number is a Perfect Cube or not
    Given a number N, the task is to write C program to check if the given number is perfect cube or not. Examples: Input: N = 216Output: YesExplanation:As 216 = 6*6*6.Therefore the cube root of 216 is 6. Input: N = 100Output: No Method 1: Naive Approach To find the cube root of the given number iterate
    5 min read
  • 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. Example Input: l = 10, r = 30Output: 11 13 17 19Explan
    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 7 Input : m = 10, n = 20Output : 11 13 17 19 Table of Content [Naive Approach] Basic Trial Division Method - O(n*n) Time and O(1) Space[Optimised A
    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 for Find largest prime factor of a number
    Given a positive integer 'n'( 1 <= n <= 1015). Find the largest prime factor of a number. Input: 6 Output: 3 Explanation Prime factor of 6 are- 2, 3 Largest of them is '3' Input: 15 Output: 5Method 1: The approach is simple, just factorise the given number by dividing it with the divisor of a
    3 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
  • C Program to Display Prime Numbers Between Intervals
    Given two numbers a and b as interval range, the task is to find the prime numbers in between this interval. Examples: Input: a = 1, b = 10 Output: 2, 3, 5, 7 Input: a = 10, b = 20 Output: 11, 13, 17, 19Approach 1: In the below program, the range of numbers is taken as input and stored in the variab
    6 min read
  • C Program for efficiently print all prime factors of a given number
    Given a number n, write an efficient function to print all prime factors of n. For example, if the input number is 12, then output should be "2 2 3". And if the input number is 315, then output should be "3 3 5 7". First Approach: Following are the steps to find all prime factors. 1) While n is divi
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences