0% found this document useful (0 votes)
40 views6 pages

Exp11 16

The document contains C program code snippets for various array and matrix operations: 1) A program to generate the Fibonacci series up to a given limit. 2) A program to find the largest element in an array. 3) A program to print array elements in reverse order. 4) A program to sort an array in ascending order. 5) A program to print the transpose of a matrix. 6) A program to find the sum of two matrices.

Uploaded by

parthivsanil
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)
40 views6 pages

Exp11 16

The document contains C program code snippets for various array and matrix operations: 1) A program to generate the Fibonacci series up to a given limit. 2) A program to find the largest element in an array. 3) A program to print array elements in reverse order. 4) A program to sort an array in ascending order. 5) A program to print the transpose of a matrix. 6) A program to find the sum of two matrices.

Uploaded by

parthivsanil
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

11)Fibonacci series

Aim:-Write C Program to generate fibonacci series up to given limit.

Program:-
#include<stdio.h>
void main()
{
int i,n , p1=0 , p2=1,s=0;
printf("Enter the limit:");
scanf("%d",&n);
printf("Fibonacci series \n %d %d ", p1, p2);
for(i=3 ; i<=n ; i++)
{
s=p1+p2;
printf("%d ",s);
p1=p2;
p2=s;
}
}

Output
Enter the limit:10
Fibonacci series
0 1 1 2 3 5 8 13 21 34

12) Largest elements in an array

Aim:-Write C program to find largest elements in an array.

Program:-
#include<stdio.h>
void main()
{
int a[50] , i , n , large;
printf("Enter the limit :");
scanf("%d",&n);
printf("Enter the array elements \n");
for(i=0 ; i<n ; i++)
{
scanf("%d", &a[i] );
}
large=a[0] ;
for(i=1 ; i<n ; i++)
{
if(large<a[i] )
large=a[i] ;
}
printf("Largest element in an array=%d", large);
}
Output
Enter the limit :5
Enter the array elements
7
6
9
4
5
Largest element in an array=9
13) Array elements in reverse order
Aim:-Write C program to print array elements in reverse order.
Program:-
#include<stdio.h>
void main()
{
int a[50],i,n;
printf("Enter the limit :");
scanf("%d",&n);
printf("Enter the array elements \n");
for(i=0;i<n;i++)
{
scanf("%d", &a[i] );
}
printf("Array elements in reverse order\n");
for(i=n-1; i>=0 ; i- -)
{
printf("%d \n", a[i]);
}
}

Output
Enter the limit :6
Enter thEnter the limit :6
Enter the array elements
6
4
5
2
8
9
Array elements in reverse order
9
8
2
5
4
6
14) Array sorting
Aim:-Write C program to sort a given array in ascending order.
Program:-
#include<stdio.h>
void main()
{
int a[50],i,n,j,temp;
printf("Enter the limit :");
scanf("%d",&n);
printf("Enter the array elements \n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0 ; i<n ; i++)
{ for(j=i+1 ; j<n ; j++)
{
if(a[ i ]>a[ j ])
{ temp=a[ i ];
a[ i ]=a[ j ];
a[ j ]=temp;
}
}
}
printf("Sorted array\n");
for(i=0 ; i<n ; i++)
{
printf("%d \n", a[i]);
}

Output
Enter the limit :5
Enter the array elements
4
7
8
6
3
Sorted array
3
4
6
7
8
15) Transpose of a matrix
Aim:-Write C program to print transpose of a matrix.
Program:-
#include<stdio.h>
void main()
{
int a[50][50],i,j,c,r;
printf("Enter the rows and columns :");
scanf("%d%d", &r , &c);
printf("Enter the array elements \n");
for(i=0 ; i<r ; i++)
{ for(j=0 ; j<c ; j++)
{ scanf("%d", &a[i][j]);
}
}
printf("Matrix is\n");
for(i=0 ; i<r ; i++)
{ for(j=0 ; j<c ; j++)
{ printf("%d\t", a[i][j]);
}
printf("\n");
}
printf("Transpose of a matrix is\n");
for(i=0 ; i<c ; i++)
{ for(j=0 ; j<r ; j++)
{ printf("%d \t ", a[j][i]);
}
printf("\n");
}
}
Output

Enter the rows and columns :2 3


Enter the array elements
123456
Matrix is
1 2 3
4 5 6
Transpose of a matrix is
1 4
2 5
3 6

16) matrix addition


Aim:-Write C program to find sum of matrices.
Program:-
#include<stdio.h>
void main()
{
int a[50][50] , b[50][50] , c[50][50] , i , j, c1, r1, c2, r2;
printf("Enter the rows and columns of first matrix :");
scanf("%d%d", &r1, &c1);
printf("Enter the rows and columns of second matrix:");
scanf("%d%d", &r2, &c2);
if(r1==r2 && c1==c2)
{
printf("Enter the array elements first matrix\n");
for(i=0 ; i<r1 ; i++)
{for(j=0 ; j<c1 ; j++)
{ scanf("%d", &a[i][j]);
}
}
printf("Enter the array elements second matrix\n");
for(i=0 ; i<r2 ; i++)
{for(j=0 ; j<c2 ; j++)
{ scanf("%d", &b[i][j]);
}
}
printf("First Matrix is\n");
for(i=0 ; i<r1 ; i++)
{ for(j=0 ; j<c1 ; j++)
{ printf("%d\t", a[i][j]);
}
printf("\n");
}
printf("second Matrix is\n");
for(i=0 ; i<r2 ; i++)
{ for(j=0 ; j<c2 ; j++)
{ printf("%d\t", b[i][j]);
}
printf("\n");
}
for(i=0 ;i<r1 ; i++)
{ for(j=0 ; j<c1 ; j++)
{ c[i][j]=a[i][j]+b[i][j];
}
}
printf("Sum of two matrices is\n");
for(i=0 ; i<r1 ; i++)
{ for(j=0 ; j<c1 ; j++)
{ printf("%d\t", c[i][j]);
}
printf("\n");
}
}
else
printf("Addition not possible");
}
Output
Enter the rows and columns of first matrix :2 2
Enter the rows and columns of second matrix:2 2
Enter the array elements first matrix
241325
Enter the array elements second matrix
653214
First Matrix is
2 4
1 3
second Matrix is
2 5
6 5
Sum of two matrices is
2 4
1 3

Enter the rows ans columns of first matrix :2 3


Enter the rows ans columns of second matrix:2 2
Addition not possible

You might also like