WAP to input a no, and check if it is prime or not.
Answers
Answered by
3
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 (n == 1)
{
printf("1 is neither a prime nor a composite number.");
}
else
{
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
}
return 0;
}
Output
Enter a positive integer: 29 29 is a prime number.
Hope this helps you
Answered by
23
Here is your answer.
class x
{
static void main(int n)
{
int c=0;
for(int I=1;I<=n;I++)
{
if(n%I==0)
c++;
}
if(c==2)
System.out.println("Prime.");
else
System.out.println("Not Prime.");
}
}
Similar questions
Science,
5 months ago
Math,
5 months ago
Math,
5 months ago
Social Sciences,
11 months ago
Social Sciences,
11 months ago
Chemistry,
1 year ago