C program for prime number

To understand better please read the following post first, it will clear your concept of prime number What is prime number and Why 1 is not a prime number?


#include<stdio.h>

int main(){
    int n, i,check;
    printf("Enter a positive number\n");
    scanf("%d",&n);
    for(i=2;i<=n/2;i++){

        if(n%i==0) {
            check = 1;
            break;
        }
    }

    if(check==1)
        printf("\n%d is not a prime number\n",n);
    else
        printf("\n%d is a prime number\n",n);

    return 0;
}

Logic: The above program takes an integer n to check whether it is prime and use two control variables i & check Since every number is divisible by 1 so we started from i = 2. And i will be up to n/2. Why n/2 and why not n Because if we divide n, by any number between n/2 and n it will give us a remainder. For example if we divide 12 by 7(12÷2 < 7) it will remain 5, in case of 9 it will remain 3. That makes if(n%i==0) false every time. So if we take n it just increase time complexity.

Then we divide n with 2 to n/2 if it divide without any remainder, we set check = 1 And show it as not a prime.

Another one


#include<stdio.h>
int main(){
    int min, max, i,count;

    printf("Enter the first end point\n");
    scanf("%d",&min);
    printf("Enter the second end point\n");
    scanf("%d",&max);
    printf("\n\n");
    for(i=min;i<=max;i++){
        for(count=2;count<=i;count++){
            if(i%count==0) break;
        }
        if(count==i) printf("%d\n",count);
    }

    return 0;
}


Here we also use the same logic to find out the prime number from an interval.


Why 1 is not a prime number?

We know that the prime numbers are 2, 3,5, 7, 11, 13, … … …, it starts from 2 because One is not a prime number.

Why one is not a prime number?

It explanation is on the following.

The fundamental theorem of arithmetic Or Factorization Theorem: The fundamental theorem of arithmetic states that every positive integer (except the number 1) can be represented in exactly one way apart from rearrangement as a product of one or more primes (MathWorld)

From the definition we got two factors

  1. product of one or more primes
  2. can be represented in exactly one way

Lets consider 12, the factors of 12 are 1, 2, 3, 4,6 and 12. So if we represent 12 as the product of primes then 12= 2x2x3
But we can not take 1x2x2x3(Extra 1 is multiplied)
Or we can’t take 1x1x2x2x3 (Extra two 1’s is multiplied)
or even we can’t take 1x1x1x2x2x3 (Extra three 1’s is multiplied). Though all these makes 12.

Thus we can represent 12 in many ways if take ‘1’ as a prime number. We cant take these because it violates our second factor "can be represented in exactly one way". So excluding the ‘1’ we represent 12 in exactly one way as a product of three primes(2,2,3).

More example 14=2x7 not (1x2x7 or 1x1x2x7)
51 =3x17 not (1x3x17 or 1x1x317)
So to maintain The fundamental theorem of arithmetic Or Factorization Theorem formula we apart ‘1’ from series of prime numbers.

But, is ‘1’ is a compound number? Nope!! as we know that compound number are those which can be represent as the product of integer number. Example 24 is a compound number as it can be written as 4x6.

So what is ‘1’ actually? According to mathematician we can define it as “Unit Number”


If anyone wish to see the Bangla version of this post See it here

Matrix addition and subtraction in C using arrays

Consider 4 matrices, A, B,C and D, we are going to add A and B and keep on C. (A+B = C). And will subtract B from A and keep it on D.(A-B=D).

First we need to check that the number of rows of matrix A is equal to the number of rows of matrix B, Also we should perform the same check in case of column. But in our program we assume that number of rows of A and B are equal, as well as the column of A is equal to column of B. Then we goes

c[00]=a[00]+b[00], c[01]=a[01]+b[01]… and so on.
d[00]=a[00]-b[00], c[01]=a[01]-b[01]… and so on.

Lets coding this simple Program.


#include<stdio.h>

int main(){
    int a[10][10],b[10][10],c[10][10],d[10][10], row, col,i,j;

    printf("Enter the rows number of both matrices\n");
    scanf("%d",&row);
    printf("Enter the columns number of both matrices\n");
    scanf("%d",&col);


        printf("Enter the elements of matrix A\n");
        for(i=0;i<row;i++){
            for(j = 0;j < col;j++){
                printf("Enter the element at [%d%d] : ",i,j);
                scanf("%d",&a[i][j]);
            }
            printf("\n");

        }

        printf("Enter the elements of matrix B\n");
        for(i=0;i<row;i++){
            for(j = 0;j < col;j++){
                printf("Enter the element at [%d%d] : ",i,j);
                scanf("%d",&b[i][j]);
            }
            printf("\n");

        }

        printf("The addition of A and B is \n\t\t");
        for(i=0;i<row;i++){
            for(j = 0;j < col;j++){
                c[i][j]=a[i][j] + b[i][j];
            }
        }
        //printing addition
        for(i=0;i<row;i++){
            for(j = 0;j < col;j++){
                printf(" %d ",c[i][j]);
            }
            printf("\n\t\t");
        }


        printf("\nThe subtraction of A and B is \n\t\t");
        for(i=0;i<row;i++){
            for(j = 0;j < col;j++){
                d[i][j]=a[i][j] - b[i][j];
            }
        }

        //printing subtraction
        for(i=0;i<row;i++){
            for(j = 0;j < col;j++){
                printf(" %d ",d[i][j]);
            }
            printf("\n\t\t");
        }



    return 0;
}