forked from itsprueba/Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprime.c
More file actions
49 lines (41 loc) · 964 Bytes
/
prime.c
File metadata and controls
49 lines (41 loc) · 964 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
38
39
40
41
42
43
44
45
46
47
48
49
/**
* C program to whether a number is prime number or not
*/
#include <stdio.h>
int main()
{
int i, num, isPrime;
/*
* isPrime is used as flag variable.
* If isPrime = 0, then number is composite
* else if isPrime = 1, then number is prime.
* Initially I have assumed the number as prime.
*/
isPrime = 1;
/* Input a number from user */
printf("Enter any number to check prime: ");
scanf("%d", &num);
for(i=2; i<=num/2; i++)
{
/* Check divisibility of num */
if(num%i==0)
{
/* Set isPrime to 0 indicating it as composite number */
isPrime = 0;
/* Terminate from loop */
break;
}
}
/*
* If isPrime contains 1 then it is prime
*/
if(isPrime == 1 && num > 1)
{
printf("%d is prime number", num);
}
else
{
printf("%d is composite number", num);
}
return 0;
}