c program to demonstrate function with argument

Description


This program demonstrates function with arguments. The program uses a function add() with thee arguments x, y and z. The variable sum which is declared inside the function finds the sum of the values passed to these arguments and the result is returned.  The statement d=add(a,b,c); calls the function, the values of a, b and c is read as input form the user.

Program


#include<stdio.h>

#include<conio.h>

int add(int x,int y,int z);

void main()

{

clrscr();

int a,b,c,d;

printf("Enter the three numbers");

scanf("%d%d%d",&a,&b,&c);

d=add(a,b,c);

printf("Sum is %d",d);

getch();

}

int add(int x,int y,int z)     //function definition

{

int sum;

sum=x+y+z;

return(sum);

}

c program to find the n'th power of a number

Description


This program prints the n'th power of a number. The value of the number and the power is read into the variables x and n. The value of r is initially set to 1. The program repeatedly multiplies r and x n times, using while loop. The result of this multiplication gives the n'th power of the given number.

Program


#include<stdio.h>

#include<conio.h>

void main()

{

int x,n,i=1,r=1;

clrscr();

printf("Enter the x & n value");

scanf("%d%d",&x,&n);

while(i<=n)

{

r=r*x;

i++;

}

printf("Value is %d",r);

getch();

}

c program to find the transpose of a matrix

Description


This program finds the transpose of a matrix.The row and column values of the matrix is read into the variables r and c, then the elements are read into the matrix a[][] using the for loop. Then the program uses another for loop to print the matrix column wise, this gives the transpose of the matrix. Below is an example of the transpose of a matrix.

Example


Original matrix

2  2  6
4  8  2
4  5  6

Transpose of the matrix

4  4  2
5  8  2
6  2  6

Program


#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

int r,c,a[50][50],i,j;

printf("Enter the row value and cloum value\n");

scanf("%d%d",&r,&c);

printf("Enter the elements of matrix\n");

for(i=0;i<r;i++)

{

for(j=0;j<c;j++)

{

scanf("%d",&a[i][j]);

}}

printf("Transpose of matrix\n");

for(j=0;j<c;j++)

{

for(i=0;i<r;i++)

{

printf("%d ",a[i][j]);

}

printf("\n");

}

getch();

}


Related post:- C++ program to find the transpose of a matrix and check whether it is symmetric or not

c program to implement matrix addition

Description


This program demonstrates matrix addition. Three 2d matrices are declared a[][], b[][] and s[][]. The row and column values of the two matrix a and b is read into the variables r and c. Then the values of the two matrices is read as input by7 using for loop.  The statement s[i][j]=a[i][j]+b[i][j]; inside the for loop performs matrix addition and the statement printf("%d ",s[i][j]); inside the for loop prints the new matrix as the output.


Program


#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int r,c,a[50][50],b[50][50],s[50][50],i,j;
printf("Enter the row value and cloum value\n");
scanf("%d%d",&r,&c);
printf("Enter the elements of first matrix\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}}
printf("Enter the elements of second matrix\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
s[i][j]=a[i][j]+b[i][j];
}}
printf("Sum of matrix\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d ",s[i][j]);
}
printf("\n");
}
getch();
}

c program to implement matrix subtraction

Description


This program demonstrates matrix subtraction.  Three 2D arrays are declared a[][], b[][] and s[][]. The row and column value of the matrix is read into the variables r and c. The program uses for loop to read the value into the two matrices a[][] and b[][]. The statement s[i][j]=a[i][j]-b[i][j]; inside the for loop performs matrix subtraction and the statement printf("%d ",s[i][j]); prints the new matrix to the output screen.


Program 


// matrix subtraction c program

#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

int r,c,a[50][50],b[50][50],s[50][50],i,j;

printf("Enter the row value and cloum value\n");

scanf("%d%d",&r,&c);

printf("Enter the elements of first matrix\n");

for(i=0;i<r;i++)

{

for(j=0;j<c;j++)

{

scanf("%d",&a[i][j]);

}}

printf("Enter the elements of second matrix\n");

for(i=0;i<r;i++)

{

for(j=0;j<c;j++)

{

scanf("%d",&b[i][j]);

}}

for(i=0;i<r;i++)

{

for(j=0;j<c;j++)

{

s[i][j]=a[i][j]-b[i][j];

}}

printf("Sum of matrix\n");

for(i=0;i<r;i++)

{

for(j=0;j<c;j++)

{

printf("%d ",s[i][j]);

}

printf("\n");

}

getch();

}