Q:

Prime number program in C

0

Prime number program in C

Prime number program in C language. A number is prime if it's divisible only by one and itself.

Two is the only even and the smallest prime number. First few prime numbers are 2, 3, 5, 7, 11, 13, 17, .... Prime numbers have many applications in computer science and mathematics. A number greater than one can be factorized into prime numbers, for example, 120 = 23*31*51 (8*3*5).

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <stdio.h>
int main()
{
  int n, i = 3, count, c;

  printf("Enter the number of prime numbers to print\n");
  scanf("%d", &n);

  if (n >= 1) {
    printf("First %d prime numbers are:\n",n);
    printf("2\n");
  }

  for (count = 2; count <= n;)
  {
    for (c = 2; c <= i - 1; c++)
    {
      if (i%c == 0)
        break;
    }
    if (c == i)
    {
      printf("%d\n", i);
      count++;
    }
    i++;
  }

  return 0;
}

output:

Enter the number of prime numbers to print

10

First 10 prime numbers are:

2

3

5

7

11

13

17

19

23

29

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now