C Program to find Matrix addition,Subtraction and trace.

Write a C program to read two matrices A (MxN) and B(MxN) and perform addition ,subtraction of A and B, and Find the trace of the resultant matrix. Output the given matrix, their sum or Differences and the trace. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include <stdio.h>
#include <conio.h>

void main()
{
int A[10][10], B[10][10], sumat[10][10], diffmat[10][10];
int i, j, M,N, option;

void trace (int arr[][10], int M, int N);

clrscr();

printf("Enter the order of the matrice A and Bn");
scanf("%d %d", &M, &N);

printf("Enter the elements of matrix An");
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
scanf("%d",&A[i][j]);
}
}

printf("MATRIX A isn");
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
printf("%3d",A[i][j]);
}
printf("n");
}

printf("Enter the elements of matrix Bn");
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
scanf("%d",&B[i][j]);
}
}

printf("MATRIX B isn");
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
printf("%3d",B[i][j]);
}
printf("n");
}

printf("Enter your option: 1 for Addition and 2 for Subtractionn");
scanf("%d",&option);

switch (option)
{
case 1: for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
sumat[i][j] = A[i][j] + B[i][j];
}
}

printf("Sum matrix isn");
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
printf("%3d",sumat[i][j]) ;
}
printf("n");
}

trace (sumat, M, N);
break;

case 2:for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
diffmat[i][j] = A[i][j] - B[i][j];
}
}

printf("Difference matrix isn");
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
printf("%3d",diffmat[i][j]) ;
}
printf("n");
}

trace (diffmat, M, N);
break;
}

} /* End of main() */

/*Function to find the trace of a given matrix and print it*/

void trace (int arr[][10], int M, int N)
{
int i, j, trace = 0;
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
if (i==j)
{
trace = trace + arr[i][j];
}
}
}
printf ("Trace of the resultant matrix is = %dn", trace);

}
Read more Similar C Programs
Matrix Programs
Learn C Programming

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program to evaluate the given polynomial P(x)=AnXn + An-1Xn-1 + An-2Xn-2+… +A1X + A0, by reading its coefficents into an array.

Write a C program to evaluate the given polynomial P(x)=AnXn + An-1Xn-1 + An-2Xn-2+… +A1X + A0, by reading its coefficents into an array.A Polynomial is a mathematical expression involving a sum of powers in one or more variables multiplied by coefficients. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/



#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define MAXSIZE 10

voidmain()
{
int a[MAXSIZE];
int i, N,power;
float x, polySum;

clrscr();

printf("Enter the order of the polynomialn");
scanf("%d", &N);

printf("Enter the value of xn");
scanf("%f", &x);

/*Read the coefficients into an array*/

printf("Enter %d coefficientsn",N+1);
for (i=0;i <= N;i++)
{
scanf("%d",&a[i]);
}

polySum = a[0];

for (i=1;i<= N;i++)
{
polySum = polySum * x + a[i];
}

power = N;

/*power--;*/

printf("Given polynomial is:n");
for (i=0;i<= N;i++)
{
if (power < 0)
{
break;
}

/* printing proper polynomial function*/
if (a[i] > 0)
printf(" + ");
else if (a[i] < 0)
printf(" - ");
else
printf (" ");
printf("%dx^%d ",abs(a[i]),power--);

}

printf("nSum of the polynomial = %6.2fn",polySum);
}

Array In C Simple C ProgramsYou can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program to find the average of largest two of the given numbers in the array.

Example programs to solve the problems of Arrays in C.
Write a C program to read in four integer numbers into an array and find the average of largest two of the given numbers without sorting the array. The program should output the given four numbers and the average with suitable headings. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/



#include <stdio.h>
#include <conio.h>
#define MAX 4

void main()
{
int a[MAX], i, l1,l2,temp;

clrscr();

printf("Enter %d integer numbersn", MAX);
for (i=0; i < MAX; i++)
{
scanf("%d", &a[i]);
}

printf("Input interger aren");
for (i=0; i < MAX; i++)
{
printf("%5d", a[i]);
}

printf("n");

l1 = a[0]; /*assume first element of array is the first largest*/
l2 = a[1]; /*assume first element of array is the second largest*/

if (l1 < l2)
{
temp = l1;
l1 = l2;
l2 = temp;
}

for (i=2;i<4;i++)
{
if (a[i] >= l1)
{
l2 = l1;
l1 = a[i];
}
else if(a[i] > l2)
{
l2= a[i];
}
}

printf("n%d is the first largestn", l1);
printf("%d is the second largestn", l2);
printf("nAverage of %d and %d = %dn", l1,l2, (l1+l2)/2);

}
Read more Similar C Programs
Array In C

Simple C Programs

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C program to input real numbers and find the mean, variance and standard deviation

Write a C program to input real numbers and find the mean, variance and standard deviation.Mean is sum of values divided by the total number of values. Variance shows the how the data s are distributed. Standard deviation is the square root of its variance. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#include <stdio.h>
#include <conio.h>
#include <math.h>
#define MAXSIZE 10

void main()
{
float x[MAXSIZE];
int i, n;
float avrg, var, SD, sum=0, sum1=0;

clrscr();

printf("Enter the value of Nn");
scanf("%d", &n);

printf("Enter %d real numbersn",n);
for(i=0; i<n; i++)
{
scanf("%f", &x[i]);
}

/* Compute the sum of all elements */

for(i=0; i<n; i++)
{
sum = sum + x[i];
}
avrg = sum /(float) n;

/* Compute varaience and standard deviation */

for(i=0; i<n; i++)
{
sum1 = sum1 + pow((x[i] - avrg),2);
}
var = sum1 / (float) n;
SD = sqrt(var);

printf("Average of all elements = %.2fn", avrg);
printf("Varience of all elements = %.2fn", var);
printf("Standard deviation = %.2fn", SD);
} /*End of main()*/
Read more Similar C Programs
Array In C

Simple C Programs

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program To Demonstrate Linear search

Write a C program to input N numbers (integers or reals) and store them in an array. Conduct a linear search for a given key number and report success or failure in the form of a suitable message.
Linear search is the basic searching algorithm, also called as sequential search. Algorithm search’s the element by comparing the each element in the list, until the desired element found. Read more about C Programming Language.

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#include <stdio.h>
#include <conio.h>

void main()
{
int array[10];
int i, N, keynum, found=0;

clrscr();

printf("Enter the value of Nn");
scanf("%d",&N);

printf("Enter the elements one by onen");
for(i=0; i<N ; i++)
{
scanf("%d",&array[i]);
}
printf("Input array isn");
for(i=0; i<N ; i++)
{
printf("%dn",array[i]);
}
printf("Enter the element to be searchedn");
scanf("%d", &keynum);

/* Linear search begins */
for ( i=0; i < N ; i++)
{
if( keynum == array[i] )
{
found = 1;
break;
}
}
if ( found == 1)
printf("SUCCESSFUL SEARCHn");
else
printf("Search is FAILEDn");

} /* End of main */
Read more Similar C Programs
C Basic

Search Algorithms.

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program To Find The Sum & Average

Example programs to solve the problems of Arrays in C.
C program to read N integers (zero, +ve and -ve) into an array A and to
a) Find the sum of negative numbers
b) Find the sum of positive numbers and
c) Find the average of all input numbers
Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include <stdio.h>
#include <conio.h>
#define MAXSIZE 10

void main()
{
int array[MAXSIZE];
int i, N, negsum=0, posum=0;
float total=0.0, averg;

clrscr();

printf ("Enter the value of Nn");
scanf("%d", &N);

printf("Enter %d numbers (-ve, +ve and zero)n", N);
for(i=0; i< N ; i++)
{
scanf("%d",&array[i]);
fflush(stdin);
}

printf("Input array elementsn");
for(i=0; i< N ; i++)
{
printf("%+3dn",array[i]);
}

/* Summing begins */
for(i=0; i< N ; i++)
{

if(array[i] < 0)
{
negsum = negsum + array[i];
}
else if(array[i] > 0)
{
posum = posum + array[i];
}
else if( array[i] == 0)
{
;
}
total = total + array[i] ;
}

averg = total / N;
printf("nSum of all negative numbers = %dn",negsum);
printf("Sum of all positive numbers = %dn", posum);
printf("nAverage of all input numbers = %.2fn", averg);

} /*End of main()*/
Read more Similar C Programs
Array In C

Simple C Programs

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C program to find the value of cos(x)

Write a C program to find the value of cos(x) using the series up to the given accuracy (without using user defined function) Also print cos(x) using library function. Read more about C Programming Language.

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                                  Happy Coding
***********************************************************/


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

void main()
{
 int  n, x1;
 float  acc, term, den, x, cosx=0, cosval;

 clrscr();

 printf("Enter the value of x (in degrees)n");
 scanf("%f",&x);

 x1 = x;

 /* Converting degrees to radians*/

 x = x*(3.142/180.0);
 cosval = cos(x);

 printf("Enter the accuary for the resultn");
 scanf("%f", &acc);
 term = 1;
 cosx = term;
 n = 1;

 do
 {
  den = 2*n*(2*n-1);
  term = -term * x * x / den;
  cosx = cosx + term;
  n = n + 1;
 } while(acc <= fabs(cosval - cosx));

 printf("Sum of the cosine series       = %fn", cosx);
 printf("Using Library function cos(%d) = %fn", x1,cos(x));
}         /*End of main() */
Read more Similar C Programs
C Basic

Search Algorithms.

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C program to find the value of sin(x)

Write a C program to find the value of sin(x) using the series up to the given accuracy (without using user defined function) Also print sin(x) using library function. Read more about C Programming Language.

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                                  Happy Coding
***********************************************************/


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

void main()
{
 int n, x1;
 float  acc, term, den, x, sinx=0, sinval;

 clrscr();

 printf("Enter the value of x (in degrees)n");
 scanf("%f",&x);

 x1 = x;

 /* Converting degrees to radians*/

 x = x*(3.142/180.0);
 sinval = sin(x);

 printf("Enter the accuary for the resultn");
 scanf("%f", &acc);

 term = x;
 sinx = term;
 n = 1;

 do
 {
  den = 2*n*(2*n+1);
  term = -term * x * x / den;
  sinx = sinx + term;
  n = n + 1;

 } while(acc <= fabs(sinval - sinx));

 printf("Sum of the sine series         = %fn", sinx);
 printf("Using Library function sin(%d) = %fn", x1,sin(x));

}         /*End of main() */
Read more Similar C Programs
C Basic

Search Algorithms.

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C program to find the GCD and LCM of two integers

Write a C program to find the GCD and LCM of two integers output the results along with the given integers. Use Euclids’ algorithm to find the GCD and LCM.
Euclids’ algorithm uses the division algorithm which repeatedly divides starting from the two numbers we want to find the GCD of until we get a remainder of 0. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                                  Happy Coding
***********************************************************/

#include <stdio.h>
#include <conio.h>

void main()
{
 int  num1, num2, gcd, lcm, remainder, numerator, denominator;
 clrscr();

 printf("Enter two numbersn");
 scanf("%d %d", &num1,&num2);

 if (num1 > num2)
 {
  numerator = num1;
  denominator = num2;
 }
 else
 {
  numerator = num2;
  denominator = num1;
 }
 remainder = num1 % num2;
 while(remainder !=0)
 {
  numerator   = denominator;
  denominator = remainder;
  remainder   = numerator % denominator;
 }
 gcd = denominator;
 lcm = num1 * num2 / gcd;
 printf("GCD of %d and %d = %d n", num1,num2,gcd);
 printf("LCM of %d and %d = %d n", num1,num2,lcm);
}  /* End of main() */
Read more Similar C Programs

Data Structures

C Sorting

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C program to find the sum of ‘N’ natural numbers.

Write a C program to find the sum of ‘N’ natural numbers. Natural numbers are the whole numbers except zero, usually we used for counting.
Example:1, 2, 3, 4,———– Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/



#include <stdio.h>
#include <conio.h>

void main()
{
int i, N, sum = 0;

clrscr();

printf("Enter an integer numbern");
scanf ("%d", &N);

for (i=1; i <= N; i++)
{
sum = sum + i;
}

printf ("Sum of first %d natural numbers = %dn", N, sum);
}
Read more Similar C Programs
Searching in C

Number Theory 
C Strings
 

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com