Find Prime Numbers from 2 To N using Sieve of Eratosthenes: C Program

Lets write a C program to find prime numbers from 2 to N, using Sieve of Eratosthenes method.

Important Note

We are working on index numbers here. Array elements will have only 2 values: 1 or 0. Wherever the value of array element is 1, that means it’s position/index number is prime orelse its composite number.

Working on array elements

This video tutorial solves text book problem statement to find prime numbers using Sieve of Eratosthenes Method: Prime Numbers using Sieve of Eratosthenes: C Program.

Visual Representation

prime numbers from 2 to 100 sieve of erathosthenes

Video Tutorial: Sieve of Eratosthenes Method To Find Prime Numbers: C Program


[youtube https://www.youtube.com/watch?v=rb5bClbN3o8]

YouTube Link: https://www.youtube.com/watch?v=rb5bClbN3o8 [Watch the Video In Full Screen.]

Source Code: Find Prime Numbers from 2 To N using Sieve of Eratosthenes: C Program

#include<stdio.h>
#include<math.h>

#define N 101

int main()
{
    int a[N] = {[0 ... N - 1] = 1}, i, j;
    int num  = sqrt(N - 1);

    for(i = 2; i <= num; i++)
    {
        if(a[i] == 1)
        {
            for(j = i * i; j < N; j += i)
                a[j] = 0;
        }
    }

    printf("Sieve of Eratosthenes\n");
    printf("Prime numbers from 2 to %d are ...\n", N - 1);
    for(i = 2; i < N; i++)
    {
        if(a[i])
            printf("%d\t", i);
    }

    return 0;
}

Output
Above code outputs all the prime numbers between 2 to N – 1.

Logic To Find Prime Numbers from 2 To N using Sieve of Eratosthenes

1. We’re using Designated Initializers to initialize all the elements of array to 1. Here we are initializing all the elements from index 0 to N – 1. If you write 0 to N, it’ll start throwing out of bounds error. As 0 to 101(if N value is 101) will be 102 elements, which exceeds the size of array.

2. Next we find square root of N – 1. Its enough to check till square root of N – 1, to get all the prime numbers – I’ve shown you the proof in the video posted above in this article.

3. Since 2 is the first prime number, we initialize i to 2 and we iterate the for loop till square root of N – 1. For each iteration increment the value of i by 1.

4. Initially we assume that all the numbers are prime, by storing 1 in all the array elements.

5. After careful observation, we come to know that the first index position we store zero for any index i is i * i. So we initialize j value to i * i. For each iteration of the inner for loop we increment the value of j by i times. Inside inner for loop we store 0 at a[j].

If we keep adding value of i to the previous value of j, then j will always have a number which is multiple of i. So we store 0 at a[j].

6. We keep repeating Step 5 until i is less than or equal to square root of N – 1.

7. Once i is equal to square root of N – 1, we print all the index numbers or position of element which has 1 in it. So these index numbers are prime numbers from 2 to N – 1.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Find Prime Factors of a Number

A positive integer is entered through the keyboard. Write a function to obtain the prime factors of this number.

For Example: Prime factors of 24 are 2, 2, 2 and 3. Whereas prime factors of 35 are 5 and 7.

Related Read:
C Program to Find Factors of a Number
C Program To Find Prime Number or Not using While Loop

Factors of a Number: All the numbers which perfectly divide a given number are called as Factors of that number.

Prime Number: Any natural number which is greater than 1 and has only two factors i.e., 1 and the number itself is called a prime number.

Note: The user entered number need not be a prime number. But the factors of the number must be prime number.

For example, 24 and 35 are not prime numbers. But the prime factors of 24 are 2, 2, 2, and 3. Here both 2 and 3 are prime numbers. Similarly, prime factors of 35 are 5 and 7. Both 5 and 7 are prime numbers.

Video Tutorial: C Program To Find Prime Factors of a Number using Function


[youtube https://www.youtube.com/watch?v=QAxnHPxicgY]

YouTube Link: https://www.youtube.com/watch?v=QAxnHPxicgY [Watch the Video In Full Screen.]


Source Code: C Program To Find Prime Factors of a Number using Function

#include<stdio.h>

void primefactors(int num)
{
    int count;

    printf("\nPrime Factors of %d are ..\n", num);
    for(count = 2; num > 1; count++)
    {
        while(num % count == 0)
        {
            printf("%d ", count);
            num = num / count;
        }
    }
    printf("\n");
}

int main()
{
    int num;

    printf("Enter a positive integer\n");
    scanf("%d", &num);

    primefactors(num);

    return 0;
}

Output 1:
Enter a positive integer
24

Prime Factors of 24 are ..
2 2 2 3

Output 2:
Enter a positive integer
35

Prime Factors of 35 are ..
5 7

Output 3:
Enter a positive integer
315

Prime Factors of 315 are ..
3 3 5 7

Output 4:
Enter a positive integer
24024

Prime Factors of 24024 are ..
2 2 2 3 7 11 13

Output 5:
Enter a positive integer
510

Prime Factors of 510 are ..
2 3 5 17

Logic To Find Prime Factors of a Number, using Function

We ask the user to enter a positive integer number and store it inside variable num. We pass this value to a function primefactors().

Inside primefactors() function we write a for loop. We initialize the loop counter to 2 – which is the smallest prime number. We exit the for loop when num is less than or equal to 1.

Inside for loop we write a while loop to check if the number is perfectly divisible by the value of count. If yes, then we display the value of count and divide the num by count and store it back into variable num.

Since we continuously modulo divide and divide the number by 2, other numbers(which are multiples of 2) can not divide the number. Similarly, we divide the number by 3, so other numbers(which are multiples of 3) can not divide the number. Next we divide the number by 5, 7, 11, etc.

Note: Function primefactors() doesn’t return anything so its return type is void. It accepts 1 integer type argument.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert