Array

Bubble Sort in C

Here you will learn about program for bubble sort in C. Bubble sort is a simple sorting algorithm in which each element is compared with adjacent element and swapped if their position is incorrect. It is named as bubble sort because same as like bubbles the lighter elements come up and heavier elements settle down. …

Bubble Sort in C Read More »

C Program to Find Sum of Diagonals of Matrix

Here is the C program to find sum of diagonal of a square matrix. #include<stdio.h> int main() { int i,j,n,d1=0,d2=0,a[5][5]; printf(“Enter size of square matrix:”); scanf(“%d”,&n); printf(“Enter Elements of matrix:\n”); for(i=0;i<n;++i) for(j=0;j<n;++j) { scanf(“%d”,&a[i][j]); if(i==j) d1+=a[i][j]; if((i+j)==(n-1)) d2+=a[i][j]; } printf(“\nFirst Diagonal Sum=%d”,d1); printf(“\nSecond Diagonal Sum=%d”,d2); return 0; }   Output Enter size of square matrix:4 …

C Program to Find Sum of Diagonals of Matrix Read More »

C Program to Insert an Element in an Array

Here is the simple C program to insert an element in a one dimensional array. The array should be in ascending order. #include<stdio.h> int main() { int a[20],n,x,i,pos=0; printf(“Enter size of array:”); scanf(“%d”,&n); printf(“Enter the array in ascending order:\n”); for(i=0;i<n;++i) scanf(“%d”,&a[i]); printf(“\nEnter element to insert:”); scanf(“%d”,&x); for(i=0;i<n;++i) if(a[i]<=x&&x<a[i+1]) { pos=i+1; break; } for(i=n+1;i>pos;–i) a[i]=a[i-1]; a[pos]=x; …

C Program to Insert an Element in an Array Read More »

C Program to Find Sum of Elements Above and Below Main Diagonal of Matrix

Here is the simple C program to find sum of elements above and below main diagonal of matrix. The matrix should be square matrix. #include<stdio.h> int main() { int i,j,m,n,d1=0,d2=0,a[5][5]; printf(“How many rows and columns:”); scanf(“%d%d”,&m,&n); printf(“Enter matrix elements:\n”); for(i=0;i<m;++i) for(j=0;j<n;++j) { scanf(“%d”,&a[i][j]); if(j>i) d1+=a[i][j]; else if(i>j) d2+=a[i][j]; } printf(\n”Sum of elements above the diagonal=%d\n”,d1); …

C Program to Find Sum of Elements Above and Below Main Diagonal of Matrix Read More »

Matrix Multiplication in C

Here is the program for matrix multiplication in C. m and n are rows and columns of first matrix. p and q are rows and columns of second matrix. Then, multiplication is possible only if n==p.   Matrix Multiplication in C #include<stdio.h> int main() { int a[5][5],b[5][5],c[5][5],m,n,p,q,i,j,k; printf(“Enter rows and columns of first matrix:”); scanf(“%d%d”,&m,&n); …

Matrix Multiplication in C Read More »

C++ Program to Find Sum of Elements Above and Below Main Diagonal of Matrix

Here is the C++ program to find sum of elements above and below the main diagonal of square matrix. #include<iostream> using namespace std; int main() { int arr[5][5],a=0,b=0,i,j,n; cout<<“Enter size of matrix(max 5):”; cin>>n; cout<<“Enter the matrix:\n”; for(i=0;i<n;++i) for(j=0;j<n;++j) cin>>arr[i][j]; for(i=0;i<n;++i) for(j=0;j<n;++j) if(j>i) a+=arr[i][j]; else if(i>j) b+=arr[i][j]; cout<<“\nSum of elements above the diagonal:”<<a; cout<<“\nSum of …

C++ Program to Find Sum of Elements Above and Below Main Diagonal of Matrix Read More »