forked from Minor-lazer/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprime number.c
More file actions
37 lines (34 loc) · 811 Bytes
/
prime number.c
File metadata and controls
37 lines (34 loc) · 811 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<stdio.h>
// declaring the recursive function
int isPrime(int, int);
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int num, prime;
printf("Enter a positive number to check if Prime: ");
scanf("%d", &num);
prime = isPrime(num, num/2);
if(prime == 1)
{
printf("\n\n%d is a prime number\n\n", num);
}
else
{
printf("\n\n%d is a Composite number\n\n", num);
}
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
// function definition
int isPrime(int n, int i)
{
if(i == 1)
return 1; // return statement terminates the recursive funtion
else
{
if(n%i == 0)
return 0;
else
isPrime(n, i-1); // recursive call not using return statement
}
}