0% found this document useful (0 votes)
8 views4 pages

Functions Program

The document contains C programs that demonstrate matrix operations, including addition and multiplication, using functions. It also includes recursive and non-recursive functions to calculate the factorial of a number, find the greatest common divisor (GCD) of two integers, and compute x raised to the power of n. Each section provides code snippets with user input and output for the respective operations.

Uploaded by

ptejaswini787
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

Functions Program

The document contains C programs that demonstrate matrix operations, including addition and multiplication, using functions. It also includes recursive and non-recursive functions to calculate the factorial of a number, find the greatest common divisor (GCD) of two integers, and compute x raised to the power of n. Each section provides code snippets with user input and output for the respective operations.

Uploaded by

ptejaswini787
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Write a C program that uses functions to perform the following:

1. Addition of Two Matrices


#include<stdio.h>
void matrixAddition(int a[10][10], int b[10][10], int sum[10][10]);
int rows, columns;
void main()
{
int a[10][10], b[10][10], sum[10][10], i, j;
printf("Enter the no of rows and columns:");
scanf("%d%d", &rows, &columns);
/* input first matrix */
printf("Enter the input for first matrix:\n");
for (i = 0; i <rows; i++)
for (j = 0; j <columns; j++)
scanf("%d", &a[i][j]);
/* input second matrix */
printf("Enter the input for second matrix:\n");
for (i = 0; i < rows; i++)
for (j = 0; j< columns; j++)
scanf("%d", &b[i][j]);
/* matrix addtion */
matrixAddition(a, b, sum);
/* print the results */
printf("\nResult of Matrix Addition:\n");
for (i = 0; i < rows; i++)
{
for (j = 0; j < columns; j++)
printf("%d\t", sum[i][j]);
printf("\n";);
}
}
/* adds two matrices and stores the output in third matrix */
void matrixAddition(int a[10][10], int b[10][10], int sum[10][10])
{
int i, j;
for (i = 0; i< rows; i++)
for (j = 0; j < columns; j++)
sum[i][j] = a[i][j] + b[i][j];
}
2. Multiplication of Two Matrices
#include <stdio.h>
void mulmatrix(int [10][10],int [10][10],int,int,int,int);
void main()
{
int m, n, p, q, i,j, a[10][10], b[10][10];
printf("Enter number of rows and columns of first matrix");
scanf("%d%d", &m, & n);
printf("Enter elements of first matrix\n");
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d",&a[i][j]);
printf("Enter number of rows and columns of first matrix");
scanf("%d%d", &p, & q);
if (n != p)
printf("The matrices can&#39;t be multiplied with each other.\n");
else
{
printf("Enter elements of second matrix\n");
for (i = 0; i < p; i++)
for (j = 0; j < q; j++)
scanf("%d",&b[i][j]);
mulmatrix(a,b,m,n,p,q);
}
}
void mulmatrix(int a[10][10],int b[10][10],int m,int n,int p,int q)
{
int i,j,k,sum=0,mul[10][10];
for (i = 0; i < p; i++){
for (j = 0; j < q; j++){
for (k = 0; k < p; k++) {
sum = sum + a[i][k]*b[k][j];
}
mul[i][j] = sum;
sum = 0;
}
}
printf("Product of the matrices:\n");
for (i = 0; i < p; i++){
for (j = 0; j < q; j++)
printf("%d" ,mul[i][j]);
printf("\n");
}
}
g. Write C programs that use both recursive and non-recursive functions
1.To find the factorial of a given integer.
#include<stdio.h>
long int rfact(int );
void fact(int);
void main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf(" recursive function of Factorial of %d = %ld", n, rfact(n));
fact(n);
}
void fact(int x)
{
int i,f=1;
for(i=1’i<=x;i++)
f=f*I;
printf("Factorial of %d = %d", n, f);
}
long int rfact (int n)
{
if (n==1)
return 1;
else
return n*factorial(n-1);
}

2.To find the GCD (greatest common divisor) of two given integers.
#include <stdio.h>
int recgcd(int x, int y);
int nonrecgcd(int x, int y);
void main()
{
int a, b, c, d;
printf("Enter two numbers a and b");
scanf("%d%d", &a, &b);
c = recgcd(a, b);
printf("The gcd of two numbers using recursion is %d\n", c);
d = nonrecgcd(a, b);
printf("The gcd of two numbers using nonrecursion is %d", d);
}
int recgcd(int x, int y){
if(y == 0){
return(x);
}
else{
return(recgcd(y, x % y));
}
}
int nonrecgcd(int x, int y){
int z;
while(x % y != 0){
z = x % y;
x = y;
y = z;
}
return(y);
}

3.To find x^n

#include <stdio.h>
int rpower(int , int);
void power(int,int)
int main() {
int base, p, result;
printf("Enter base number:\n ");
scanf("%d", &base);
printf("Enter power number(positive integer):\n ");
scanf("%d", &p);
result = power(base, p);
printf("%d^%d = %d\n", base, a, result);
power(base,p);
return 0;
}
void power(int base,int p)
{
int i,re=1;
for(i=1;i<=p;i++)
re=re*base;
printf("%d^%d = %d\n", base, a, re);
}
int power(int base, int p) {
if (p != 0)
return (base * power(base, p - 1));
else
return 1;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy