C Program To Check Prime Number or Not
A prime number is a whole number greater than 1 whose only factors are 1 and itself.
A factor is a whole number that can be divided evenly into another number.
The first few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23 and 29.
Numbers that have more than two factors are called composite numbers.
Note: The number 1 is neither prime nor composite.
Example :
Given Number : 11
It is a Prime Number
Since :
11 whose only factors are 1 and itself (11).
Given Number : 10
It is Not a Prime Number
Since :
10 Have more than two factors are called composite numbers
you can use the following algorithm:
- Read the input number.
- Iterate from 1 to the input number.
- For each iteration,
check if the number is divisible by the current iteration value. - If it is divisible by the current iteration value.
Increase count by 1 - Check value of count = 2, If it is Equal to 2
It is Prime Number otherwise It is Not a Prime Number(Composite Number) - Print the result.
In this algorithm,It iterates from 1 to the number and checks if the number is divisible by any value in that range. If it is divisible, increase the value of count by 1 Check value of count = 2, If it is Equal to 2,It is Prime Number otherwise It is Not a Prime Number(Composite Number)
Please note that this implementation assumes that the input number is a positive integer. You may need to add additional checks for negative numbers or other cases depending on your requirements.
Here is an example implementation in C:
Source Code
// C Program To Check Prime Number - Part One
#include<stdio.h>
#include<conio.h>
void main()
{
int num , i , count = 0 ;
printf("Enter a Number To Check : ");
scanf("%d",&num);
for( i=1; i<=num; i++ )
{
if( num%i==0 )
{
printf("%d ",i);
count++;
}
}
if(count==2)
printf("\nIt is a Prime Number . ");
else
printf("\nIt is Not a Prime Number . ");
getch();
}
Output
Sample Input :
Enter a Number To Check : 11
Sample Output:
1 11
It is a Prime Number
Sample Input :
Enter a Number To Check : 10
Sample Output:
1 2 5 10
It is Not a Prime Number
you can use the following algorithm:
- Read the input number.
- initialize check to 0
- Iterate from 2 to half of the input number.
- For each iteration,
check if the number is divisible by the current iteration value. - If it is divisible by the current iteration value.
initialize check to 1 and break the loop - Check value of check = 0, If it is Equal to 0
It is Prime Number otherwise It is Not a Prime Number(Composite Number) - Print the result.
In this algorithm,It iterates from 2 to half of the number and checks if the number is divisible by any value in that range. If it is divisible, initialize check to 1 and break the loop, Check value of check = 0, If it is Equal to 0,It is Prime Number otherwise It is Not a Prime Number(Composite Number)
Please note that this implementation assumes that the input number is a positive integer. You may need to add additional checks for negative numbers or other cases depending on your requirements.
Here is an example implementation in C:
Source Code
// C Program To Check Prime Number - Part Two
#include<stdio.h>
#include<conio.h>
void main()
{
int num , i ;
int check=0;
printf("Enter a Number To Check : ");
scanf("%d",&num);
for( i=2; i<=num/2; i++ )
{
if( num%i==0 )
{
check=1;
break;
}
}
if(check==0)
printf("It is a Prime Number . ");
else
printf("It is Not a Prime Number . ");
getch();
}
Output
Sample Input :
Enter a Number To Check : 11
Sample Output:
It is a Prime Number
Sample Input :
Enter a Number To Check : 10
Sample Output:
It is Not a Prime Number
Looping Related Programs
- C Program to Check Perfect Number or Not
- C Program to Check Twin Prime Number or Not
- C Program to Check Duck Number or Not
- C Program to Check Niven Or Harshad Number or Not
- C Program to Check Spy Number or Not
- C Program to Check Palindrome Number or Not
- C Program to Check Neon Number or Not
- C Program to Check Armstrong Number or Not
- C Program to Check Strong (Krishnamurthy)Number or Not
- C Program to Check Happy Number or Not
- C Program to Check Automorphic Number or Not