Finding Out Prime Numbers – Programming Code Explained


18 May, 2020  2:54pm – 19 May, 8:45pm
--------------
We know that the prime numbers are the numbers that can be divisible only by 1 or itself. 1 is not a prime number ( or not composite number). So a prime number must be a natural number greater than 1.
For example, 7 is a prime number because it is divided exactly only by 1 (7/1=7) and by 7 itself (7/7=1). It’s not divisible by any other numbers, like -
7/2=3.5,
7/3=2.3,
7/4=1.75,
7/5=1.4,
7/6=1.16 etc.
So 7 is a prime number.
Let’s take 8.
8/1 = 8,
8/8 = 1,
8/4 = 2,
8/2 = 4
It is divisible by 1 and 8 including 2 and 4. So it’s not  prime; it is a composite number.
Notice that if a number is divided only two times (by 1 and by the number itself), then it is prime. And if a number is divided more than two times (by 1, by the number itself and by any other numbers), then it is not prime. It is a composite number. And this is the main concept of our programming we’re going to code.
Okay, now, suppose we want to find out the prime numbers from 2 to 100.
Let’s see the programme code.



#include<stdio.h>

void main(){
    int i, j, n;
    
    printf("Enter the number till which you want prime numbers\n");
    scanf("%d", &n);
    
    printf("Prime numbers are:-\n");    
    for(i=2; i<=n; i++){

        int count = 0;
        for( j=1; j<=i; j++ ){
            if( i%j == 0 ){
                count++;
            }
        }
         
       if( count == 2 ){
            printf("%d ",i);
        }
    }
}


-
This might not be an efficient programming. Actually the main purpose of this tutorial is to explain the inner process of the code so a fresher can be able to understand any other complex programming code later on.
Okay, so, first we will take an integer input, i.e., n = 100. That means we want to throw out the prime numbers from 2 to 100.
Then we will initiate two nested loops. Like outer loop i-loop and inner loop j-loop.
i-loop will be iterated from 2 to 100. (i = 2, i <= 100)
This is because we’re going to go through from 2 to 100 to throw out the prime numbers.
Then we take a variable ‘count’ to count how many times the number is divisible.
j-loop will check if the number is divisible, and count how many times it is divisible. Look,  this loop will be iterated from 1 to the number, i.e., j = 1, j <= i. Here i is the number itself. But, why i?
Well, let’s take 9.
9/1 = 9; divisible
9/2 = 4.5; not divisible
9/3 = 3; divisible
9/4 = 2.25; not divisible
9/5 = 1,8; not divisible
9/6 = 1.5; not divisible
9/7 = 1.285; not divisible
9/8 = 1.125; not divisible
9/9 = 1; divisible
We need to divide 9 by 1 through 9. So here i is 9. And 9 is divisible three times. So 9 is not prime. If you’re not clear still now, just go over the following demonstration. Better you use pen and paper. The programming code should be kept next to this tutorial so you can see the code and the tutorial simultaneously.

 

 
 
 



Likewise, the i-loop will be going on up to 100 or whatever number you input. If you want to find out up to 50, then input 50 (n=50).
But at this stage we found i = 2, 3, 5, 7,…..
And these are the prime numbers.

Comments

Popular Posts