Prime Number in C: A Beginner’s Guide

Prime Number in C: A Beginner’s Guide

8 mins read2.1K Views Comment
Updated on Nov 29, 2023 14:01 IST

A prime number is a whole number that is divided by 1, and the number itself has only two factors. Non-prime numbers are called composite numbers like 4, 6, 9, and more. Examples of prime numbers are 1, 2, 3, 7, 11, 13, 17, etc. 2 is the only even prime number.

Finding whether a large number is prime or not takes time and becomes calculative. With the C program, we can identify prime numbers with simple steps. C is the simplest language among all programming languages. My journey in programming languages started from C itself, and we can say C is the foundation programming language.  

You can also explore: What is Do While Loop in C?

In this article, we will use the C language to write a program with three different approaches to determine if a number is prime or not. 

Table of Contents

  1. Prime Number Program Using for loop 
  2. Prime Number Program Using While Loop 
  3. Prime Number Program Using Function 
  4. Conclusion 
Recommended online courses

Best-suited C / C++ courses for you

Learn C / C++ with these high-rated online courses

4 K
2 months
– / –
6 months
– / –
1 month
3.5 K
3 months
15 K
2 months
– / –
50 hours
– / –
40 hours
– / –
2 months
– / –
4 months
5.5 K
140 hours

Prime Number Program Using for loop 

We will write a C program using a for loop to determine if the input number is prime or not. 

Syntax of for loop 

for (initialization; condition; increment/decrement) {
    // Code to execute on each iteration
}

For example 


 
for (i = 1; i <= 5; i++) {
printf("for loop\n");
}
Copy code

Algorithm 

Step 1: Take a number as input from the user. 

Step 2: initialize the variable x =1. 

Step 3: Check the condition of the for loop. 

Step 4: If a number is a prime, it will not be divisible by a loop variable. The value of x remains the same. 

Step 5: If the input number is divisible by the loop variable, the value of x increases by 1. 

Implementation of the Prime Number Program


 
#include <stdio.h>
#include <math.h> // Include math library for sqrt function
int main() {
int number, i;
int isPrime = 1; // Flag to check prime status
printf("Enter any number: ");
scanf("%d", &number);
if (number <= 1) {
isPrime = 0; // Numbers less than or equal to 1 are not prime
} else {
for (i = 2; i <= sqrt(number); i++) {
if (number % i == 0) {
isPrime = 0;
break; // Break the loop if a factor is found
}
}
}
if (isPrime == 1) {
printf("%d is a Prime number\n", number);
} else {
printf("%d is not a Prime number\n", number);
}
return 0;
}
Copy code

Output

Enter any number: 5
5 is a Prime number

Explanation:

In the above code, the for loop will work till the loop variable (i) is less than equal to the input number. When a number enters a loop, it will check modulo, and if the input number is not divisible by the loop variable, it is a prime and terminates the loop. If a number is divisible by a loop variable, then the number is not prime and increases the value of the local variable x by 1. 

Storage classes in C

For Example:  

  • The user input number is 5.
  • When program control enters the for loop, it's variable i = 2. Checking loop condition (i <= number): 2 <= 5 (2 is less than or equals 5), yes, 2 is less than 5. 
  • Increasing the value of i by 1. The input number satisfied the loop condition, and it entered the loop. 
  • Checking if condition (number % i == 0): 5%2 is not equal to 0. Hence, the condition failed, and the loop was terminated. Now, i = 3 
  • Again, check for loop condition 3 <= 5, yes, and the value of i is 4. 
  • Checking if condition 5%3 = 0? No. 
  • Entering for loop and checking condition: 4 <= 5? Yes, and i will increase by 1 and become 5. 
  • Checking if condition 5%4 = 0? No.  
  • Entering for loop and checking its condition: 5<=5? Yes, and i will increase by 1 and become 6. 
  • Checking if condition 5%5 = 0? Yes, the Value of the x variable will increase by 1, and now x = 1. 
  • Entering the for loop and checking its condition: 6<=5? No, the loop terminates, and program control comes out of the for loop. 
  • Checking if condition (if x == 1) is true as the value of x is increased and now x is equal to 1.  
  • if the condition is satisfied and the program will process the print statement. 
  • The output is prime. 

Factorial Programs in C – A Step-by-Step Guide

Prime Number Program using While Loop 

In this section, we will create a C program to determine if the input number is prime or not using a while loop. 

While loop: it is another important loop of C programming language and is different from for loop. Mostly it is used for Boolean value conditions.  

In the while loop, the loop variable is initialized outside and before the start of the loop. 

Syntax of While Loop


 
while (condition) {
// Code to execute as long as the condition is true
}
Copy code

For example


 
while (x != 0) {
printf("This is a while loop\n");
x++; // This will increment x, but the loop will not execute as x is initialized to 0
}
Copy code

Implementing C program for prime number 


 
#include <stdio.h>
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
// Checking for negative input
if (n < 0) {
printf("Factorial of a negative number doesn't exist.\n");
} else {
unsigned long long factorial = 1; // Long long to handle larger numbers
int i = 1;
while (i <= n) {
factorial *= i; // factorial = factorial * i
i++; // Incrementing the loop variable
}
printf("Factorial of %d = %llu\n", n, factorial);
}
return 0;
}
Copy code

Output

Enter a positive integer: 5
Factorial of 5 = 120

Learning About Pointers in C

Prime Number Program Using Function 

To find whether the input number is prime or not we are using another approach: creating a user-defined function prime_Number. 

In C language, programmers can create their functions and call that functions in the main() function. 

Implementing C program using User-Defined Function


 
#include <stdio.h>
// User-defined function to check if the number is prime
int isPrime(int number) {
if (number <= 1) return 0; // Numbers less than or equal to 1 are not prime
for (int i = 2; i * i <= number; i++) {
if (number % i == 0) return 0; // If number is divisible by i, it's not prime
}
return 1; // If no divisors found, number is prime
}
// Main function
int main() {
int number;
printf("Enter any number: ");
scanf("%d", &number);
// Calling the function
if (isPrime(number)) {
printf("\n%d is a Prime Number\n", number);
} else {
printf("\n%d is Not a Prime Number\n", number);
}
return 0;
}
Copy code

Output

Enter the number: 11 
11 is a Prime Number 

All About While Loop in C

Explanation 

In the above code, we defined a function with the name prime_Number(). A function makes it easy to use the code anywhere in the program just by calling the function name. In the prime_Number() function, we defined local variables and used a for loop to check whether a number is prime. It will return the value of x.  

In the main() function (which controls the program), call the defined function by typing its name. By the value of x, the program will decide the output. 

Implementing Arrays in C Programming

Conclusion 

There are various ways to write the same program. In this article, we learned three different approaches (for, while, and function) to write a program to check if the input number is prime.  C is an easy and versatile language, providing various ways to use the same logic. User-defined functions in C help coders from repetitive codes and time wastage. 

I hope you find this article helpful in understanding the logic of the prime number program. 

Happy Learning!!

Author: Sonal Meenu Singh

FAQs

What is a prime number in C?

Prime Number is a whole number that has only two factors, 1 and the number itself. It is easy to determine prime numbers verbally from 1 to 100. The problem starts when the number is very large, like a 3 or 4-digit number. We can use a C program to find whether the input number is prime or not by using very simple logic.

What is the logic to find prime number?

The most basic logic is finding the modulo of the input number. Here is the algorithm for finding whether the input number is prime or not using the C language. Step 1: Take the input number from the user to find its prime or not. Step 2: Declare a for loop to check input number satisfies the loop condition: if yes. Step 3: Find the modulo of the input number with the loop variable. Step 4: Check if condition: modulo of number and loop variable. Step 5: If the input number is divided more than 1 time by the loop variable, the number is not prime. Step 6: else number is prime.

How to show prime numbers in C?

Prime numbers can be found in C by using loops. One can use any loop like for or while to implement the logic of finding prime numbers. The logic is that a prime number is divisible by 1 and itself only.

How do I print prime numbers?

Prime numbers are numbers that are divisible by themselves. For example 1, 2, 3, 5, 7, etc. You can print prime numbers by using for, while loop in C with the properties of prime numbers.

What is prime formula?

In math, a prime number can be written as 6n + 1 or 6n -1. Where n is any number. In c or any other language, the formula for finding a prime number is the number should not be divided by any other number except 1.

What is the easiest way to find primes?

There are various ways to determine if a given number is prime or not. The ease of a method depends from person to person. In C, for loop is an easy way to find a prime number program. The type of method also depends on the requirement of the program.

What is perfect number in C?

A perfect number is a number that is equal to the sum of its positive divisors. That means if we divide a number, say 6, its divisors are 1, 2, and 3. When we combine all together, we will get the number itself i.e., 6.

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio