In this Program, you’ll learn how to find Prime number entered by User.
What is the prime number?
To understand this example, you should have knowledge of C programming :
A number is considered as the prime number when it satisfies the below conditions.
- It should be the whole number
- It should be greater than 1
- It should have only 2 factors. They are 1 and the number itself.
Example for prime numbers: 2, 3, 5, 7, 11, 13, 17, 19, 23 etc.
Why 4, 6, 8, 9, 10, 12, 14, 15, 16 etc are not prime numbers?Advertisement
Because the number 4 can be factored as 2*2 and 1*4. As per the rule of a prime number, there should be 2 factors only. They are 1 and the number itself. But, number 4 has 2*2 also. Like this, all remaining numbers 6, 8, 9, 10, 12, 14, 15, 16 have factors other than 1 and the number itself. So, these are not called as prime numbers.
Program to find Prime Numbers Entered by User
#include <stdio.h>
int main()
{
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d",&n);
for(i=2; i<=n/2; ++i)
{
// condition for nonprime number
if(n%i==0)
{
flag=1;
break;
}
}
if (flag==0)
printf("%d is a prime number.",n);
else
printf("%d is not a prime number.",n);
return 0;
}
Output:
Please enter a number: 13
Entered number is 13 and it is a prime number.

Related C Programs
- C program for factorial
- C program for Fibonacci series
- C Program to Print an Integer (Entered by the User)
- C Program to check whether the given Square Matrix is symmetric or not.
- C Program to check whether a two-dimensional array is a Sparse Matrix
- C Program to Add Two Integers
- C Program to check Numbers is Even or Odd.
- C Program to Reverse a Sentence using Recursion.
- C Program to Find the size of int, float, double and char.
- C Program to Multiply two floating-point Numbers.
- C Program to Implement stack.
- C Program to Implement circular linked list List.
- C Program to calculate compound interest.
Ask your questions and clarify your/others doubts on C Prime Numbers by commenting. Documentation