WAP to check whether the given number is a prime number or not

Write a program to check whether the given number is a prime number or not using recursive function

#include<stdio.h>
#include<conio.h>

int prime(int,int);

int main() {
	int n,count;
	printf("Enter an integer: ");
	scanf("%d",&n);

	count = prime(n,2);

	if(count == 1)
		printf("%d is a prime",n);
	else
		printf("%d is not a prime",n);
	getch();
	return 0;

}

int prime(int n, int i) {
	static int count=0;
	if(n==1) 
		return 1;
	else if(i>n)
		return count;
	else {
		if(n%i==0) {
			count++;
			return prime(n,i+1);
		}
		else
			return prime(n,i+1);
	}
}

Output

Enter an integer: 5
count = 1
5 is a prime

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments