0% found this document useful (0 votes)
17 views

Lab7 Update

Uploaded by

rupinidevi3005
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)
17 views

Lab7 Update

Uploaded by

rupinidevi3005
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/ 6

Lab 7: Simple functions

1. write a program to find whether the given integer is prime or not using type-1,2,3,4
functions.
2. Write a program to find the addition of two matrices using functions.
3. Write a program to find the multiplication of two matrices using functions.
4. Write a program to find the transpose of a matrix using functions.
Program Code:
1. write a program to find whether the given integer is prime or not using type-1,2,3,4
functions.

Type 1: Function with no arguments and no return Values.


#include <stdio.h>
void prime();
int main()
{
prime(); // argument is not passed
return 0;
}
/* return type is void meaning doesn't return any value*/
void prime()
{
int n, i, count = 0;
printf("Enter a positive integer value: ");
scanf("%d",&n);
for(i=2; i < n; i++)
{
if( n%i == 0)
count = 1;
}
if (count == 1)
printf("%d is not a prime number.", n);
else
printf("%d is a prime number.", n);
}

Type 2: Function with arguments and no return values.


#include <stdio.h>
void prime(int n);
int main()
{
int n;
prime(n);
}
// Function with arguments and no return values.
void prime(int n)
{
int i, count = 0;
printf("Enter a positive integer value: ");
scanf("%d",&n);
for(i=2; i < n; i++)
{
if( n%i == 0)
count=1;
}
if (count == 1)
printf("%d is not a prime number.", n);
else
printf("%d is a prime number.", n);
}
Type 3: Function with arguments and return values.
#include <stdio.h>
int prime(int n);
int main()
{
int n,c;
c=prime(n);
if (c == 1)
printf("The given number is not a prime number.");
else
printf("The given number is a prime number.", n);
} // end of main
// Function with arguments and return values.
int prime(int n)
{
int i, count = 0;
printf("Enter a positive integer value: ");
scanf("%d",&n);
for(i=2; i < n; i++)
{
if( n%i == 0)
count=1;
}
return count;
}
Type 4: Function with no arguments and return values.
#include <stdio.h>
int prime();
int main()
{
int c;
c=prime();
if (c == 1)
printf("The given number is not a prime number.");
else
printf("The given number is a prime number.");
} // End of main()
// Function with no arguments and return values
int prime()
{
int n, i, count = 0;
printf("Enter a positive integer value: ");
scanf("%d",&n);
for(i=2; i < n; i++)
{
if( n%i == 0)
count=1;
}
return count;
}
2. Write a program to find the addition of two matrices using functions.
#include<stdio.h>
main()
{
int a[10][10],b[10][10],i,j,m,n,p,q,ch;
void read(int [10][10],int ,int );
void display(int [10][10],int ,int );
void add(int [10][10],int [10][10],int,int);
printf("Enter size of Matrix-A\n");
scanf("%d%d",&m,&n);
printf("Enter size of Matrix-B\n");
scanf("%d%d",&p,&q);
read(a,m,n);
read(b,p,q);
if(m==p && n==q)
add(a,b,m,n);
else
printf("Addition not possible\n");
}
// function to read matrix elements
void read(int x[10][10],int m,int n)
{
int i,j;
printf("enter Array elements\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&x[i][j]);
}
// function to print matrix elements
void display(int x[10][10],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d\t",x[i][j]);
printf("\n");
}
}
// function to perform the addition of two matrices
void add(int a[10][10],int b[10][10],int m,int n)
{
int i,j,c[10][10];
for(i=0;i<m;i++)
for(j=0;j<n;j++)
c[i][j]=a[i][j]+b[i][j];
display(c,m,n);
}

3. Write a program to find the multiplication of two matrices using functions.


#include<stdio.h>
main()
{
int a[10][10],b[10][10],i,j,m,n,p,q,ch;
void read(int [10][10],int ,int );
void display(int [10][10],int ,int );
void mul(int [10][10],int [10][10],int,int,int);
printf("Enter size of Matrix-A\n");
scanf("%d%d",&m,&n);
printf("Enter size of Matrix-B\n");
scanf("%d%d",&p,&q);
read(a,m,n);
read(b,p,q);
if(n==p)
mul(a,b,m,q,n);
else
printf("Multiplication not possible\n");

// function to read matrix elements


void read(int x[10][10],int m,int n)
{
int i,j;
printf("enter Array elements\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&x[i][j]);
}
// function to print matrix elements
void display(int x[10][10],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d\t",x[i][j]);
printf("\n");
}
}
// function to perform the multiplication of two matrices
void mul(int a[10][10],int b[10][10],int m,int q,int n)
{
int i,j,k,c[10][10];
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
display(c,m,q);
}

4. Write a program to find the transpose of a matrix using functions.


#include<stdio.h>
main()
{
int a[10][10],b[10][10],i,j,m,n,p,q,ch;
void read(int [10][10],int ,int );
void display(int [10][10],int ,int );
void transpose(int [10][10],int,int);
printf("Enter size of Matrix-A\n");
scanf("%d%d",&m,&n);
read(a,m,n);
transpose(a,m,n);
}

// function to read matrix elements


void read(int x[10][10],int m,int n)
{
int i,j;
printf("enter Array elements\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&x[i][j]);
}
// function to print matrix elements
void display(int x[10][10],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d\t",x[i][j]);
printf("\n");
}
}
// function to find the transpose of matrix
void transpose(int a[10][10],int m,int n)
{
int i,j,c[10][10];
for(i=0;i<m;i++)
for(j=0;j<n;j++)
c[j][i]=a[i][j];
display(c,n,m);
}

You might also like