Introduction :
The prime number is a fundamental and important concept in mathematics. Prime numbers play an important role in many areas of mathematics and computer science particularly in number theory, cryptography and algorithms. The prime number is also used in many real-world applications such as generating secure encryption keys in cryptography, testing the performance and efficiency in algorithms and solving mathematical problems.
In this article, I shall show you how to create various types of prime number related programs in C programming language.
What is prime number :
A prime number is a positive natural number (integer) that greater than 1 and only divides by 1 and itself. For example 2, 3, 5, 7, 11 and so on are all prime numbers. 0 or 1 is not a prime number. All prime numbers are odd numbers but 2 is the smallest and only even prime number.
How to check a number is prime or not :
To check a given number is prime or not, you have to divide the number by all the number from 2 to the less than the given number one by one. If the number is not divisible by one of these numbers, it is a prime number. If it is divisible by one of these numbers, it is not a prime number.
How run the program :
All the programs, I have run in VS Code. If you do not install VS Code on your PC, click here. Then, open VS Code and create new file like “pac.c”. After that, copy the below code and paste in the “pac.c” file. Now, save the file and run the program on your PC.
Different types of program related to prime number :
In this article, I shall discuss you the following programs of prime number using the C programming language.
Find a number is prime or not using C programing language :
In this program, I show you how to find given number is prime or not in C programing language. First, you have to declare two integer variables such as “num” and “i”. Then, ask the user to enter an integer number to check the number is prime or not using printf() function. Using scanf() function, store the number in “num” variable. After that, using “if” statement you can check the number 0 or 1.
Then, using “else if” statement you have to check the number positive or not. After that, run a while loop in “else” statement. In the while loop, find the remainder of the number divided by 2 to less than the number. In the process, if the remainder would be 0, the number is not prime otherwise the number is prime. You can copy the below code and use it in your own program.
/* Developd by Puskar Jasu*/
#include <stdio.h>
int main()
{
int num, i;
printf("Enter a integer number to check prime or not\n");
i = 2;
scanf("%d", &num);
if ((num == 0) || (num == 1))
{
printf("%d is not prime number\n", num);
}
else if (num < 0)
{
printf("%d is nagetive number, please enter a positive number\n", num);
}
else
{
while (i <= num - 1)
{
if (num % i == 0)
{
printf("%d is not prime number\n", num);
break;
}
i++;
}
}
if (num == i)
printf("%d is prime number\n", num);
return 0;
}
Output :
After running this above program on your PC, you can see the following image as output of finding a number is prime or not in C programming language.

Display Prime Numbers Between Two Intervals using C programming language :
In this program, I shall learn you how to display all the prime numbers between two intervals in C programming language. First, you have to declare five int variables as “num1”, “num2”, “i”, “j”, “first_num” and also take an int array “arr[100]”. Using printf() function, ask the user for entering two numbers for display all prime numbers between them.
Now, using scanf() function store the value in the “num1” and “num2” variables respectively. You can store the value of “num1” in “first_num” variable for further use. After that, using “if” statement you can check the number must be positive number. Then, using “else if” statement you can check second number must be greater than 1. Using another “else if” statement you check first number must be less than second number or not.
Now, run two while loop in “else” statement. Using the inner and outer while loop, find the prime number one by one between the intervals. You have to store all the prime numbers in “arr” array. In this process, you get all the prime numbers among them. Now, print all the prime numbers using another while loop. You can copy the below code and use in your own program.
/* Developd by Puskar Jasu*/
#include <stdio.h>
int main()
{
int num1, num2, i, j, first_num;
int arr[1000];
printf("\nEnter two number for display all prime number between them\n");
scanf("%d", &num1);
scanf("%d", &num2);
first_num = num1;
j = 0;
if ((num1 < 0) || (num2 < 0))
{
printf("Please enter two positive numbers\n");
}
else if (num2 == 0 || num2 == 1)
{
printf("Second number must be greater than 1\n");
}
else if (num1 > num2)
{
printf("First number must be less than second number\n");
}
else
{
while (num1 <= num2)
{
i = 2;
while (i <= num1 - 1)
{
if (num1 % i == 0)
{
break;
}
i++;
}
if (num1 == i)
{
arr[j] = num1;
j++;
}
num1++;
}
i = 0;
printf("Prime number from %d to %d are ", first_num, num2);
while (i < j)
{
printf("%d ", arr[i]);
i++;
}
printf("\n");
}
return 0;
}
Output :
If you run this code of program, you can see the following output of “display Prime Numbers Between Two Intervals in C programming language”.

Count and sum of all prime numbers between two intervals using C programming language :
In this program, I shall show you how to count all prime numbers between two intervals and sum of all prime numbers in C programming language. To run this program, you have to declare seven integer variables such as “num1”, “num2”, “i”, “j”, “first_num”, “count” and “sum”. Then, ask the user for entering two numbers for count and total of all prime numbers between them using printf() function.
Now, store the value of numbers in “num1” and “num2” variables using scanf() function. In “first_num” variable, store the value of “num1” variable for next usage. After that, using “if” statement you can check the number positive number or not. Then, using “else if” statement you can check second number must be greater than 1. Using another “else if” statement you have to check first number must be less than second number or not.
Now, run two while loop in “else” statement. In the inner and outer while loop, find the prime number one by one between the intervals. In “count” and “sum” variables, you can store the number of prime numbers and total of prime number respectively. In this process, you can get the count and total of all the prime numbers. Now, print count of the prime numbers and total sum using printf() function. You can copy the below code and use in your own program.
/* Developd by Puskar Jasu*/
#include <stdio.h>
int main()
{
int num1, num2, i, j, first_num, count = 0, sum = 0;
printf("\nEnter two number for count and total of all prime number between them\n");
scanf("%d", &num1);
scanf("%d", &num2);
first_num = num1;
j = 0;
if ((num1 < 0) || (num2 < 0))
{
printf("Please enter two positive numbers\n");
}
else if (num2 == 0 || num2 == 1)
{
printf("Second number must be greater than 1\n");
}
else if (num1 > num2)
{
printf("First number must be less than second number\n");
}
else
{
while (num1 <= num2)
{
i = 2;
while (i <= num1 - 1)
{
if (num1 % i == 0)
{
break;
}
i++;
}
if (num1 == i)
{
count++;
sum = sum + num1;
j++;
}
num1++;
}
i = 0;
printf("Total prime number from %d to %d are %d and sum of all prime numbers are %d\n", first_num, num2,
count, sum);
}
return 0;
}
Output :
If you run this code of program, you can see the following output of count and sum of all prime numbers between two intervals in C programming language.

Display all prime numbers from a list of number using C programming language :
In this program, I shall show you how to display all the prime numbers from a list of number in C programming language. First, you have to declare four integer variables (“i”, “j”, “k” and “total_num”) and also take two int array (“arr[100]” and “prime[100]”). Then, using printf() function ask the user to enter how many numbers he wants to input for check prime number. Using scanf() function, store the value in “total_num” variable.
You can use scanf() function within while loop to take all the numbers of list from the user and store in “arr” array. Using outer and inner while loop, check all the numbers from “arr” array and store the prime number in “prime” array. Now, print all the prime numbers from “prime” array using another while loop. If you want to use this code in your program, just copy it.
/* Developd by Puskar Jasu*/
#include <stdio.h>
int main()
{
int i, j, k, total_num;
int arr[100], prime[100];
printf("\nHow many numbers you want to input for check prime number\n");
scanf("%d", &total_num);
if (total_num <= 0)
{
printf("You must be input greater than 0\n");
}
else
{
printf("Enter the numbers\n");
i = 0;
while (i < total_num)
{
scanf("%d", &arr[i]);
i++;
}
j = 0;
k = 0;
while (j < total_num)
{
i = 2;
while (i <= arr[j] - 1)
{
if (arr[j] % i == 0)
{
break;
}
i++;
}
if (arr[j] == i)
{
prime[k] = arr[j];
k++;
}
j++;
}
if (k > 0)
{
i = 0;
printf("Prime number are ");
while (i < k)
{
printf("%d ", prime[i]);
i++;
}
printf("\n");
}
else
{
printf("There are no prime number\n");
}
}
return 0;
}
Output :
If you run this code of program, you can see the following output of “display all prime numbers from a list of number in C programming language”.

Check a number can be expressed as the sum of two prime numbers using C language :
In this program, I show you how to check a number can be expressed as the sum of two prime numbers in C programming language. First, you have to declare seven int variable such as “num”, “i”, “j”, “k”, “l”, “m” and “n”. Then, ask the user to enter an integer number for express sum of two prime numbers using printf() function. Using scanf() function, store the number in “num” variable.
After that, using “if” statement you have to check the input number greater than 3 or not. Then, run outer and inner while loop into for loop in “else” statement. Here, I check every number less than the input number. If the number is prime than check the subtraction of input number and the number for prime numbers. If both number and subtraction are prime, print the numbers using printf() function. You can copy the below code and use in your own program.
/* Developd by Puskar Jasu*/
#include <stdio.h>
int main()
{
int num, i, j, k, l, m, n = 0;
printf("Enter a positive integer for express sum of two prime numbers: ");
scanf("%d", &num);
if (num < 4)
{
printf("Please enter greater than 3\n");
}
else
{
j = 2;
for (k = 0; k <= num / 2; k++)
{
m = num - j;
i = 2;
while (j <= num / 2)
{
if (j % i == 0)
{
break;
}
i++;
}
if (j == i)
{
l = 2;
while (l <= m)
{
if (m % l == 0)
{
break;
}
l++;
}
if (m == l)
{
n++;
printf("The number %d is express sum of two prime numbers %d and %d\n", num, j, m);
}
}
j++;
}
if (n == 0)
{
printf("The number %d is not express as sum of two prime numbers\n", num);
}
}
return 0;
}
Output :
If you run this code of program, you can see the following output of check a number can be expressed as the sum of two prime numbers in C programming language.

Conclusion :
In this article, I discuss about various math problem of prime number that solve using C programming language. After follow my article, you have learn how to check prime number, how to display all prime numbers from a number to another number, how many prime number are there and a total of all prime numbers.
You can also learn how find prime number from a list of numbers. Here, you see how check a number can be expressed as the sum of two prime numbers. I think you can use this code for a better program. Thanks for visiting my site. Thank you for visiting my site.