Following program reads a number from the user and prints whether it is prime or not by using a flag variable. A flag variable is a binary variable with values 0 and 1:
/*Program to find the given number is prime number or not using a flag variable*/
main()
{
int number,i,flag=0;
clrscr();
printf("Enter the number:");
scanf("%d",&number);
for(i=2;i<=number-1;i++)
{
if(number%i==0)
{
flag = 1;
}
}
if(flag==1)
{
printf("\n%d is not a prime number",number);
}
else if(number<=1)
{
printf("\n%d is invalid number!",number);
}
else
{
printf("\n%d is a prime number",number);
}
getch();
}
Sample input and output:
Enter the number:17
17 is a prime number
No comments:
Post a Comment