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...

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 product of one or more primes can be represented...

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],...

C program to find sum of diagonal elements in a matrix

Row-major order and Column-major order: In computing, row-major order and column-major order describe methods for arranging multidimensional arrays in linear storage such as memory. The difference is simply that in row-major order, consecutive elements of the rows of the array are contiguous in memory; in column-major order, consecutive elements of the columns are contiguous.(Wiki) C Supports Row-major order: That means if we represents a two dimensional matrix in C, At first It will traverse the first row then second row and so...

Matrix multiplication in C with explanation

We should have basic knowledge on the representation of two dimensional matrix in C. Here is three two dimensional matrix a, b and c. a for matrix A, b for matrix B, and c for matrix C. which holds the multiplication result. i, j, k are control variable and sum is needed to hold the summation of the consecutive two products. #include <stdio.h> int main() { int a[2][2] = {3,4,2,7}; int b[2][2] = {6,8,5,9}; int c[2][2], i, j, k, sum; printf("Matrix A is : \n\t\t"); for(i=0;i<2;i++){ for(j=0;j<2;j++){ ...

How to convert a while loop to a for loop in c

The basic structure of for loop is as following for(initialization; condition; adjustment){         statement(s);       } So the fo loop has four parts, Initialization: We need to assign a value for the control variable. condition: We have to use a conditional expression here. The compiler execute/iterate the for loop until it get 0 or false adjustment: Adjust the control variable (by increment or decrement ) so that the condition get 0 or false statement(s): Any type of simple or compound statement(s) The basic structure of while loop is as following while(condition){...

Difference between while loop and if statement

The basic structure of while loop is as following while(condition){         statement;       } The basic structure of if statement is as following if(condition){         statement;       } So, seeing their structure, it seems that they are same. But no,,, they are not same. if the condition of if is true, the if statement will execute. Otherwise not. Once the condition is true it will execute and will iterate. Where as the while loop will iterate until it get a false or '0' #include...

Factorial program in c using for loop, while loop and function.

Wikipedia define factorial as following In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. Math is Fun gives the following definition The factorial function (symbol: !) means to multiply a series of descending natural numbers. Example 4! = 4 × 3 × 2 × 1 = 24 7! = 7 × 6 × 5 × 4 × 3 × 2 × 1 = 5040 1! = 1 0! = 1, because the following reason. We can rearrange one thing only one way, as 0 is also an entity so we can rearrange it in one way. Also...