Skip to content
Home » C Programs » C Program to Generate Prime Numbers upto n

C Program to Generate Prime Numbers upto n

Basic prime number generation in c. A prime number is a number larger than one that is not the product of two lesser natural numbers. A composite number is a natural number greater than 1 that is not prime. 

We utilise two for loops, one for counting up to n and the other for checking whether the integer is prime or not. The technique is therefore the same as determining if an integer is prime or not.

Prime Number Definition in C

Prime numbers consist of only two factors: one and themselves. The first five prime numbers, for example, are 2, 3, 5, 7, and 11. Composite numbers, on the other hand, have more than two variables. 

Composite Number in C

A composite number is a positive integer that isn’t one of the prime numbers and has more than one factor. The first five composite numbers are known as “composites” for short. They are 4, 6, 8, 9, 10, and 12. 

For Loop and Its Execuetion Procedure

A for loop is a repetition control structure that allows us to create a loop that runs a set number of times. The loop allows us to do an arbitrary number of steps in a single line. A loop variable is used to regulate the loop in for loop.

A loop in C is made up of two parts: the loop body and the control statement. The control statement is a set of conditions that tells the loop’s body to keep running until the provided condition is false. The C loop’s goal is to repeat the same code several times. Using for loop we have successfully executed the prime number generation in c

Prime Number Generation in C

//prime number generation in c
void main()
int i,j,n;
printf("Enter the number for prime number you want\n");
printf("Prime numbers are\n"); 
int c=0;
printf("%d ",i);

Output of prime number generation in c:

Enter the number for prime number you want
Prime numbers are
2 3 5 7 11 13 17 19 23 29 31 37 41 43