forked from gouravthakur39/beginners-C-program-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Prime.c
29 lines (28 loc) · 766 Bytes
/
Prime.c
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
//A c program to print prime numbers from 1 to n
#include<stdio.h>
#include<math.h>
void main(){
int num, i, j,check;
printf("Enter number: ");
scanf("%d",&num);
printf("Prime numbers are:\n");
//traversing all numbers from 2 to entered integer num
for(i=2; i<num; i++)
{
//loop to check divisibility
for(j=2; j<i; j++)
{
//checking if it is divisible by any smaller number
check=sqrt(i);
if(check%j == 0)
{
//if it is divisible exiting from loop
break;
}
}
//printing the number if it is never divisible by numbers smaller than itself
if(i == j){
printf("%d\n", i);
}
}
}