#include<stdio.h>
#include<conio.h>
int prime(int number)
{
int k,rem,flag;
flag=1;
k=2
while(k<=number-1)
{
rem=number%k;
if(rem==0)
{
flag=0;
break;
}
k++;
}
return(flag);
}
void main()
{
int number;
clrscr();
printf("Enter a number
");
scanf("%d",&number);
if(prime(number))
printf("%d is prime",number);
else
printf("%d is not prime",number);
getch();
}
OUTPUT
Enter a number
6
6 is not a prime number
Enter a number
7
7 is not a prime number
EXPLANATION
Function prime() is defined with one formal argument of int type and is made to return either 1 or 0.The purpose of the function is to take an integer number and find out whether the number is prime or not. If the number is prime, it returns 1.Otherwise it returns 0.
In the main(),an integer number is accepted into the variable n. A call is made to the function prime() by passing the value of n.Since the function the function returns either 1 or 0, the function call has been used as a conditional expression as if (prime(n)) in main().If prime(n) returns 1 then the number is displayed as prime. Otherwise, the number is displayed as not prime.
#include<conio.h>
int prime(int number)
{
int k,rem,flag;
flag=1;
k=2
while(k<=number-1)
{
rem=number%k;
if(rem==0)
{
flag=0;
break;
}
k++;
}
return(flag);
}
void main()
{
int number;
clrscr();
printf("Enter a number
");
scanf("%d",&number);
if(prime(number))
printf("%d is prime",number);
else
printf("%d is not prime",number);
getch();
}
OUTPUT
Enter a number
6
6 is not a prime number
Enter a number
7
7 is not a prime number
EXPLANATION
Function prime() is defined with one formal argument of int type and is made to return either 1 or 0.The purpose of the function is to take an integer number and find out whether the number is prime or not. If the number is prime, it returns 1.Otherwise it returns 0.
In the main(),an integer number is accepted into the variable n. A call is made to the function prime() by passing the value of n.Since the function the function returns either 1 or 0, the function call has been used as a conditional expression as if (prime(n)) in main().If prime(n) returns 1 then the number is displayed as prime. Otherwise, the number is displayed as not prime.
0 comments:
Post a Comment