What is prime Number?
A prime number is a positive integer which is divisible only by 1 and itself only.
Examples of Prime Numbers:
2, 3, 5, 7, 11, 13 etc.
The following program tells you how to check whether the number is prime or not?
Solution :C
// A C program to check whether the number is prime or not
// Important question to prepare for Vyomlabs Interview Preparation
#include <stdio.h>
void prime(int n);
int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
prime(n);
return 0;
}
void prime (int n) {
int i, flag = 0;
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);
}
A prime number is a positive integer which is divisible only by 1 and itself only.
Examples of Prime Numbers:
2, 3, 5, 7, 11, 13 etc.
The following program tells you how to check whether the number is prime or not?
Solution :C
// A C program to check whether the number is prime or not
// Important question to prepare for Vyomlabs Interview Preparation
#include <stdio.h>
void prime(int n);
int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
prime(n);
return 0;
}
void prime (int n) {
int i, flag = 0;
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);
}