EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Prime Numbers in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » Prime Numbers in C

prime number in c

Introduction to Prime Numbers in C

A prime number is a finite numerical value that is higher than 1, and that can be divided only by 1 and itself. A few of the prime numbers starting in ascending order are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, etc. In C programming, there are a few possible operations involving the prime numbers like ‘to find if the given number is a prime number or not’, ‘to display all the prime numbers inside a given range’, ‘to display the prime numbers below a specific value’, and ‘to display the prime numbers above a specific value’. These scenarios can be coded in C programming using the conditional statements and looping statements, such as for loop, if else condition and while loop.

Examples to Implement Prime Numbers in C

In this section, we are going to discuss a few programs to check prime numbers using C language.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Example #1

Program to check prime number in C using for loop.

Code:

#include<stdio.h>
#include<conio.h>
int main()
{
int num, i, count = 0, m;
printf("Enter the number: ");
scanf("%d",&num);
m = num / 2;
for(i = 2; i <= m; i++)
{
if(num % i == 0)
{
printf("Entered number is not prime");
printf("\n");
count = 1;
break;
}
}
if(count == 0)
{
printf("Entered number is prime");
printf("\n");
}
return 0;
}

Output:

Prime Numbers in C-1.1

using for loop

Code Explanation:

Here we have written a program to check prime number using for loop. We have used four variables, variable num is used to allow a user to enter the value. Variable i is used to check the condition, variable count is used to set a counter value. and variable m is used to check the mathematical calculation.

Example #2

Program to check prime number in C using while loop.

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,570 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

Code:

#include<stdio.h>
#include<conio.h>
int main()
{
int num, i = 2, count = 0;
printf("Enter the number: ");
scanf("%d",&num);
while(i <= sqrt(num))
{
if(num % i == 0)
{
count = 1;
break;
}
i++;
}
if(count == 0)
{
printf("Entered number is prime");
}
else
{
printf("Entered number is not prime");
printf("\n");
}
return 0;
}

Output:

Prime Numbers in C-2.1

Prime Numbers in C-2.2

Code Explanation:

Here we have written a program to check prime number using while loop. Here we have used three variables num, i and count. The #include<math.h> library is used to perform mathematical functions. In this program, we make use of the sqrt() function to find out the square root of the number.

In this program, first, it asks a user to enter a number. Then the entered number is copied into num. Here num is used to compare the result with the original. while condition checks whether the number is greater than 0 or not. If the number is greater than 0, it will execute the statements following while. Then it will check for the condition num % i == 0.

Example #3

Program to check prime number in C using a do while loop.

Code:

#include<stdio.h>
#include<conio.h>
#include <math.h>
int main()
{
int num, i = 2, count = 0;
printf("Enter the number: ");
scanf("%d",&num);
do
{
if(num % i == 0)
{
count = 1;
break;
}
i++;
}
while(i <= sqrt(num));
if(count == 0)
{
printf("Entered number is prime");
printf("\n");
}
else
{
printf("Entered number is not prime");
printf("\n");
}
return 0;
}

Output:

prime number in C

prime number

Code Explanation:

Here we have written a program to check prime number using a do-while loop. Here we have written a program to check prime number using a do-while loop. Here we have used three variables num, i and count. The #include<math.h> library is used to perform mathematical functions. In this program, we make use of the sqrt() function to find out the square root of the number.

In this program, first, it asks a user to enter a number. Then the entered number is copied into num. Here num is used to compare the result with the original. while condition checks whether the number is greater than 0 or not. if the number is greater than 0, it will execute the statements following while. Then it will check for the condition num % i == 0. The only difference in the above example that it first checks the condition i.e. i <= sqrt(num) and here in this example the same condition is tested at the end of the loop.

Example #4

Program to print prime number between two intervals in C using while loop.

Code:

#include<stdio.h>
#include<conio.h>
int main()
{
int lower_limit, upper_limit, i, count;
printf("Enter the lower limit: ");
scanf("%d",&lower_limit);
printf("Enter the upper limit: ");
scanf("%d",&upper_limit);
printf("List of prime numbers between " );
printf("%d",lower_limit);
printf(" and ");
printf("%d",upper_limit);
printf("\n");
while(lower_limit < upper_limit)
{
count = 0;
for(i = 2; i <= lower_limit/2; ++i)
{
if(lower_limit % i == 0)
{
count = 1;
break;
}
}
if(count == 0)
printf("%d",lower_limit);
printf("\n");
++lower_limit;
}
return 0;
}

Output:

Lower limit

Recommended Articles

This is a guide to Prime Numbers in C. Here we discuss what is prime number along with programs to check whether the number is prime or not using various loops. You may also have a look at the following articles to learn more –

  1. Prime Numbers in C#
  2. Prime Number in Python
  3. Prime Numbers in Java
  4. Prime Number in JavaScript

C Programming Training (3 Courses, 5 Project)

3 Online Courses

5 Hands-on Projects

34+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training (3 Courses, 5 Project) Learn More