Writing effecient programs
These students who have studied C in their engineering classes, will hardly write a function. They are scared of functions. I will show you how a function can make the program easier to write
The function returns a boolean value 1 if the number is prime and 0 if it is not. If the number as
#include<stdio.h>
int isPrime(int);
int main()
{
int n;
printf("Enter a number:");
scanf("%d",&n);
if (isPrime(n))
printf("%d is prime number\n",n);
else
printf("%d is not a prime number \n",n);
return 0;
}
int isPrime(int n)
{
int i;
for(i=2;i<i++)
if (n%i==0)
return 0;/* not a prime number*/
return 1;/* No factor Hence prime number
}
The function returns a boolean value 1 if the number is prime and 0 if it is not. If the number as
Comments
Post a Comment