Practical-07
Write a function prime that returns 1 if its argument is a prime and return zero Otherwise.
Introduction
Code
Practical-07.c
int prime(int n) {
for(int i = 2; i * i <= n; i++) { // loop many times as needed
if (n % i == 0) { // no remainder means not prime
// not prime
}
}
return 1; // prime
}
Output
Enter a number: 5
The number is prime
Explanation
The prime() function takes a number as an argument and returns 1 if the number is prime and 0 if the number is not prime. The main() function takes the number from the user and calls the prime() function. The prime() function returns 1 if the number is prime and 0 if the number is not prime. The main() function prints the result.