24. Write a program to check whether the given number is prime or not.
#include <stdio.h>
#include<conio.h>
void main()
{
int a,b,val=0;
printf("Enter a positive integer: ");
scanf("%d", &a);
for(b = 2; b <= a/2; ++b)
{
// condition for nonprime number
if(a%b == 0)
{
val = 1;
break;
}
}
if (b == 1)
{
printf("1 is neither a prime nor a composite number.");
}
else
{
if (val == 0)
printf("%d is a prime number.",a);
else
printf("%d is not a prime number.",a);
}
getch();
}
#OUTPUT:
#include<conio.h>
void main()
{
int a,b,val=0;
printf("Enter a positive integer: ");
scanf("%d", &a);
for(b = 2; b <= a/2; ++b)
{
// condition for nonprime number
if(a%b == 0)
{
val = 1;
break;
}
}
if (b == 1)
{
printf("1 is neither a prime nor a composite number.");
}
else
{
if (val == 0)
printf("%d is a prime number.",a);
else
printf("%d is not a prime number.",a);
}
getch();
}
#OUTPUT:
Comments
Post a Comment