Prime Number in C
Q. Write a C program to check Prime Number or Not.
#include
void main()
{
int n, i, flag = 0;
printf("Enter the number: ");
scanf("%d", &n);
for(i = 2; i <= n/2; i++)
{
if(n%i == 0)
{
flag = 1;
break;
}
}
if ( n<=1)
{
printf("Prime number is greater than 1.");
}
else
{
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
}
}
Q. Write an algorithm to check Prime Number or Not.
1. Start 2. Prompt the user to enter an integer `n`. 3. Read the input value `n`. 4. If `n` is less than or equal to 1: - Print "Prime number is greater than 1." - Stop. 5. Initialize a flag variable `flag = 0` to indicate whether a divisor is found. 6. For `i` from 2 to `n/2`: - If `n % i == 0` (i.e., `n` is divisible by `i`): - Set `flag = 1` (not prime) - Break the loop 7. After the loop: - If `flag` is 0: - Print "`n` is a prime number." - Else: - Print "`n` is not a prime number." 8. End
Quickly Find What You Are Looking For
OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.
point.com