C Program for Prime Numbers

Estudies4you

Prime Numbers Code in C language

1 #include<stdio.h>
2 int main()
3 {
4 int n, i = 3, count, c;
5 printf("Enter the number_of_prime numbers_required\n");
6 scanf("%d",&n);
7 if ( n >= 1 )
8 {
9 printf("First %d prime_numbers are :\n",n);
10 printf("2\n");
11 }
12 for ( count = 2 ; count <= n ;  )
13 {
14 for ( c = 2 ; c <= i - 1 ; c++ )
15 {
16 if ( i%c == 0 )
17 break;
18 }
19 if ( c == i )
20 {
21 printf("%d\n",i);
22 count++;
23  }
24 i++;
25  }
26 return 0;
27 }

C Programming Code for Prime Number or Not

1 #include<stdio.h>
2 main()
3 {
4 int n, c = 2;
5 printf("Enter a_number_to_check_if it is_prime\n");
6 scanf("%d",&n);
7 for ( c = 2 ; c <= n - 1 ; c++ )
8 {
9 if ( n%c == 0 )
10 {
11 printf("%d is not_prime.\n", n);
12 break;
13 }
14 }
15 if ( c == n )
16 printf("%d is prime.\n", n);
17 return 0;
18 }

C Program Code for Prime Number By Using Functions

1 #include<stdio.h>
2 int check_prime(int);
3 main()
4 {
5 int n, result;
6 printf("Enter_an_integer to check_whether it_is prime_or_not.\n");
7 scanf("%d",&n);
8 result = check_prime(n);
9 if ( result == 1 )
10 printf("%d is prime.\n", n);
11 else
12 printf("%d is not_prime.\n", n);
13 return 0;
14 }
15 int check_prime(int a)
16 {
17 int c;
18 for ( c = 2 ; c <= a - 1 ; c++ )
19 {
20 if ( a%c == 0 )
21 return 0;
22 }
23 if ( c == a )
24 return 1;
25 }

Click Here to Download C Programming Lab Experiments

To Top