Computer Science, asked by baina3109, 28 days ago

Write a function Is_Prime that has an input parameter i.e num, and returns a
value of 1 if num is prime, otherwise returns a value of 0.​


write its program for C language

Answers

Answered by anindyaadhikari13
6

Answer:

Here comes the C program for the question.

#include <stdio.h>

int is_Prime(int num) {

for(int i=2;i<num;i++) {

if(num%i==0)

return 0;

}

return 1;

}

int main() {

printf("%d\n",is_Prime(2));

printf("\n",is_Prime(10));

}

A number is said to be prime if it is only divisible by 1 and itself.

Here, the loop iterates in the range 2 to number - 1. If any number in this range is divisible by the number entered through the parameter, then the number is not prime. In that case, it returns 0. After iteration, I wrote - return 1 i.e., if the number doesn't return 0, then it must be prime. In that case, it returns 1.

For verification, I have called the function in main() method and the output is displayed.

2 is prime, 1 is returned.

10 is not prime, 0 is returned.

Refer to the attachment.

•••♪

Attachments:
Similar questions