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...
Taylor and Maclaurin Series
Lets try with Taylor and Maclaurin Series. We start by supposing that ƒ is any function that can be represented by a power series:
Let’s try to determine what the coefficients cn must be in terms of ƒ. To begin, notice that if
we put x = a in Equation 1, then all terms after the first one are 0 and we get
ƒ(a) = c0
If we differentiate the series in equation 1 term by term...
Here we just convert the degree in radian as we know that
1800 = πc
And the substitution of x = a in equation 2
ƒ'(a) = c1
Now we differentiate both sides...
C program For Trigonometric ratio.

Lets try with the library function to calculate the trigonometric ratio. Here use the built in function. The C library function double cos(double x) returns the cosine of a radian angle x. Correspondingly the sin(x) and tan(x)
Here we just convert the degree in radian using 1800 = πc (Radian)
// Using Library function
#include<stdio.h>
void main(){
float tempt, degree;
printf("Enter the number \(In degree\)\n");
scanf("%f",°ree);
tempt = degree;
degree = degree * 3.14159265359 / 180;
printf("sin\(%.2f\)...
1's and 2's complement

1’s Complements: Just replaces each one with ‘0’ and each zero with ‘1’. For example: 100101 it’s 1’s complement is 011010
2’s Complements: When we add an extra one with the 1’s complements of a number we will get the 2’s complements. For example: 100101 it’s 1’s complement is 011010 and 2’s complement is 011010+1 = 011011 (By adding one with 1's complement)
Why we need the complements of a number
One reason of it is to represent the negative number. In binary system we use ‘1’ for negative number and ‘0’ for positive number. We...
C program for Bitwise Operation.
To know more about Bitwise operation please read the Bitwise Operator in C. It explains how these bitwise operator work in C
#include <stdio.h>
void main(void)
{
int i=165,j=281;
printf("Bitwise AND operation of %d and %d is %d \n", i,j, i&j);
printf("Bitwise OR operation of %d and %d is %d \n", i,j, i|j);
printf("Bitwise XOR operation of %d and %d is %d \n", i,j, i^j);
printf("2 bits right shift of %d is %d \n", i, i>>2 );
printf("3 bits left shift of %d is %d \n", i, i<<3);
printf("NOT operation of %d is %d \n", i, ~i);
}
...
Bitwise Operator in C

In the C programming language, operations can be performed on a bit level using bitwise operators.
Following are the six bitwise operators provided by C for bit manipulation
Symbol
Operator
&
bitwise AND
|
bitwise inclusive OR
^
bitwise exclusive OR
<<
left shift
>>
right shift
~
bitwise NOT (one's complement) (unary)
To understand these operation lets ahead with two example.. We know that in C, integer consumed 2 bytes or 16 bit memory. So the binary representation of 165 and 281 as the following...
C program and flowchart to find the largest of three numbers

Following is the flowchart to find out the largest of three number
And following is the C code
#include <stdio.h>
void main(void)
{
int A,B,C;
printf("Enter 3 integer number \n");
scanf("%d",&A);
scanf("%d",&B);
scanf("%d",&C);
if(A>B){
if(A>C){
printf(" %d is the Greatest Number \n",A);
}
else{
printf("%d is the greatest Number \n",C);
}
}
else{
if(B>C){
printf("%d is the greatest Number...
To check if it is Leap Year with C
#include <stdio.h>
void main(void)
{
int year;
printf("Enter a year to check if it is Leap Year\n");
scanf("%d",&year);
if(year%400==0) /* Why mod 400 */
printf("%d is a Leap Year\n",year);
else if(year%100==0) /* Why mod 100 */
printf("%d is not a Leap Year\n",year);
else if(year%4==0)
printf("%d is a Leap Year\n",year);
else
printf("%d is not a Leap Year\n",year);
}
...
The reason for leap year

Who invented Leap Years?
Julius Caesar introduced Leap Years in the Roman empire over 2000 years ago, but the Julian calendar had only one rule: any year evenly divisible by 4 would be a leap year. This led to way too many leap years, but didn't get corrected until the introduction of the Gregorian Calendar more than 1500 years later.
Which Years are Leap Years?
In the Gregorian calendar 3 criteria must be taken into account to identify leap years:
The year is evenly divisible by 4;
If the year can be evenly divided by 100, it is...
What is Modulo operation?

In computing, the modulo operation finds the remainder of division of one number by another (sometimes called modulus).
Given two positive numbers, a (the dividend) and n (the divisor), a modulo n (abbreviated as a mod n) is the remainder of the division of a by n .
For instance, the expression "5 mod 2" would evaluate to 1 because 5 divided by 2 leaves a quotient of 2 and a remainder of 1.
While "9 mod 3" would evaluate to 0 because the division of 9 by 3, has a quotient of 3 and leaves a remainder of 0; there is nothing to subtract...
Find out Odd and Even number using C
C program tofind out odd or even number using modulus operator
#include <stdio.h>
void main(void)
{
int i;
printf("Enter an integer\n");
scanf("%d",&i);
if(i%2==0)/* Modulo operator Here it produce 0 for every even Number */
{
printf("This is an even number.\n");
}
else
{
printf("This is an odd Number\n");
}
...