You are on page 1of 72

//1.C Program to Calculate Average Using Arrays.

#include <stdio.h>
int main() {
int n, i;
float num[100], sum = 0.0, avg;

printf("Enter the numbers of elements: ");


scanf("%d", &n);

while (n > 100 || n < 1) {


printf("Error! number should in range of (1 to
100).\n");
printf("Enter the number again: ");
scanf("%d", &n);
}

for (i = 0; i < n; ++i) {


printf("%d. Enter number: ", i + 1);
scanf("%f", &num[i]);
sum += num[i];
}

avg = sum / n;
printf("Average = %.2f", avg);
return 0;
}

Output:
Enter the numbers of elements: 6
1. Enter number: 45.3
2. Enter number: 67.5
3. Enter number: -45.6
4. Enter number: 20.34
5. Enter number: 33
6. Enter number: 45.6
Average = 27.69
__________________________________________
__________________
//2.C Program to Find Largest Element of an Array.
#include <stdio.h>
int main() {
int n;
double arr[100];
printf("Enter the number of elements (1 to 100): ");
scanf("%d", &n);

for (int i = 0; i < n; ++i) {


printf("Enter number%d: ", i + 1);
scanf("%lf", &arr[i]);
}

// storing the largest number to arr[0]


for (int i = 1; i < n; ++i) {
if (arr[0] < arr[i]) {
arr[0] = arr[i];
}
}

printf("Largest element = %.2lf", arr[0]);

return 0;
}

Output:
Enter the number of elements (1 to 100): 5
Enter number1: 34.5
Enter number2: 2.4
Enter number3: -35.5
Enter number4: 38.7
Enter number5: 24.5
Largest element = 38.70
__________________________________________
____________________
//3.C Program to Calculate Standard Deviation.
// SD of a population
#include <math.h>
#include <stdio.h>
float calculateSD(float data[]);
int main() {
int i;
float data[10];
printf("Enter 10 elements: ");
for (i = 0; i < 10; ++i)
scanf("%f", &data[i]);
printf("\nStandard Deviation = %.6f",
calculateSD(data));
return 0;
}
float calculateSD(float data[]) {
float sum = 0.0, mean, SD = 0.0;
int i;
for (i = 0; i < 10; ++i) {
sum += data[i];
}
mean = sum / 10;
for (i = 0; i < 10; ++i) {
SD += pow(data[i] - mean, 2);
}
return sqrt(SD / 10);
}

Output:
Enter 10 elements: 1
2
3
4
5
6
7
8
9
10

Standard Deviation = 2.872281


__________________________________________
__________________________

//4.C program to convert Decimal to Hexadecimal


number system.
// C Program to demonstrate Decimal to
Hexadecimal
// Conversion using the format specifier
#include <stdio.h>
int main()
{
int decimalNumber = 45;

// printing hexadecimal number


// using format specifier %X
printf("Hexadecimal number is: %X",
decimalNumber);
return 0;
}

Output:
Hexadecimal number is: 2D
__________________________________________
_________________________

//5.C program to convert Binary to Hexadecimal


number system.
/*
* C Program to Convert Binary to Hexadecimal
*/
#include <stdio.h>

int main()
{
long int binaryval, hexadecimalval = 0, i = 1,
remainder;

printf("Enter the binary number: ");


scanf("%ld", &binaryval);
while (binaryval != 0)
{
remainder = binaryval % 10;
hexadecimalval = hexadecimalval + remainder
* i;
i = i * 2;
binaryval = binaryval / 10;
}
printf("Equivalent hexadecimal value: %lX",
hexadecimalval);
return 0;
}

Output:
Enter the binary number: 10000
Equivalent hexadecimal value: 10
__________________________________________
__________________________
//C Program to Add Two Matrix Using Multi-
dimensional Arrays.
#include <stdio.h>
int main() {
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows (between 1 and
100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1
and 100): ");
scanf("%d", &c);

printf("\nEnter elements of 1st matrix:\n");


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}

printf("Enter elements of 2nd matrix:\n");


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element b%d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}
// adding two matrices
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
sum[i][j] = a[i][j] + b[i][j];
}

// printing the result


printf("\nSum of two matrices: \n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", sum[i][j]);
if (j == c - 1) {
printf("\n\n");
}
}

Output:
Enter the number of rows (between 1 and 100): 2
Enter the number of columns (between 1 and 100):
3

Enter elements of 1st matrix:


Enter element a11: 2
Enter element a12: 3
Enter element a13: 4
Enter element a21: 5
Enter element a22: 2
Enter element a23: 3
Enter elements of 2nd matrix:
Enter element b11: -4
Enter element b12: 5
Enter element b13: 3
Enter element b21: 5
Enter element b22: 6
Enter element b23: 3
Sum of two matrices:
-2 8 7

10 8 6
__________________________________________
__________________________
//11.C Program to Multiply to Matrix Using Multi-
dimensional Arrays.
#include <stdio.h>

// function to get matrix elements entered by the user


void getMatrixElements(int matrix[][10], int row, int
column) {

printf("\nEnter elements: \n");

for (int i = 0; i < row; ++i) {


for (int j = 0; j < column; ++j) {
printf("Enter a%d%d: ", i + 1, j + 1);
scanf("%d", &matrix[i][j]);
}
}
}

// function to multiply two matrices


void multiplyMatrices(int first[][10],
int second[][10],
int result[][10],
int r1, int c1, int r2, int c2) {

// Initializing elements of matrix mult to 0.


for (int i = 0; i < r1; ++i) {
for (int j = 0; j < c2; ++j) {
result[i][j] = 0;
}
}

// Multiplying first and second matrices and storing


it in result
for (int i = 0; i < r1; ++i) {
for (int j = 0; j < c2; ++j) {
for (int k = 0; k < c1; ++k) {
result[i][j] += first[i][k] * second[k][j];
}
}
}
}

// function to display the matrix


void display(int result[][10], int row, int column) {

printf("\nOutput Matrix:\n");
for (int i = 0; i < row; ++i) {
for (int j = 0; j < column; ++j) {
printf("%d ", result[i][j]);
if (j == column - 1)
printf("\n");
}
}
}

int main() {
int first[10][10], second[10][10], result[10][10], r1,
c1, r2, c2;
printf("Enter rows and column for the first matrix:
");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for the second
matrix: ");
scanf("%d %d", &r2, &c2);
// Taking input until
// 1st matrix columns is not equal to 2nd matrix row
while (c1 != r2) {
printf("Error! Enter rows and columns again.\n");
printf("Enter rows and columns for the first
matrix: ");
scanf("%d%d", &r1, &c1);
printf("Enter rows and columns for the second
matrix: ");
scanf("%d%d", &r2, &c2);
}

// get elements of the first matrix


getMatrixElements(first, r1, c1);

// get elements of the second matrix


getMatrixElements(second, r2, c2);
// multiply two matrices.
multiplyMatrices(first, second, result, r1, c1, r2,
c2);

// display the result


display(result, r1, c2);

return 0;
}

Output:
Enter rows and column for the first matrix: 2
3
Enter rows and column for the second matrix: 3
2

Enter elements:
Enter a11: 2
Enter a12: -3
Enter a13: 4
Enter a21: 53
Enter a22: 3
Enter a23: 5

Enter elements:
Enter a11: 3
Enter a12: 3
Enter a21: 5
Enter a22: 0
Enter a31: -3
Enter a32: 4

Output Matrix:
-21 22
159 179
__________________________________________
_____________________
//12.C Program to Find Transpose of a Matrix.
#include <stdio.h>
int main() {
int a[10][10], transpose[10][10], r, c;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);

// asssigning elements to the matrix


printf("\nEnter matrix elements:\n");
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}

// printing the matrix a[][]


printf("\nEntered matrix: \n");
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
printf("%d ", a[i][j]);
if (j == c - 1)
printf("\n");
}

// computing the transpose


for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
transpose[j][i] = a[i][j];
}

// printing the transpose


printf("\nTranspose of the matrix:\n");
for (int i = 0; i < c; ++i)
for (int j = 0; j < r; ++j) {
printf("%d ", transpose[i][j]);
if (j == r - 1)
printf("\n");
}
return 0;
}

Output:
Enter rows and columns: 2
3

Enter matrix elements:


Enter element a11: 1
Enter element a12: 4
Enter element a13: 0
Enter element a21: -5
Enter element a22: 2
Enter element a23: 7
Entered matrix:
1 4 0
-5 2 7
__________________________________________
__________________________
//13.C Program to Multiply two Matrices by Passing
Matrix to a Function.
#include <stdio.h>

void enterData(int firstMatrix[][10], int


secondMatrix[][10], int rowFirst, int columnFirst, int
rowSecond, int columnSecond);
void multiplyMatrices(int firstMatrix[][10], int
secondMatrix[][10], int multResult[][10], int rowFirst,
int columnFirst, int rowSecond, int columnSecond);
void display(int mult[][10], int rowFirst, int
columnSecond);

int main()
{
int firstMatrix[10][10], secondMatrix[10][10],
mult[10][10], rowFirst, columnFirst, rowSecond,
columnSecond, i, j, k;

printf("Enter rows and column for first matrix: ");


scanf("%d %d", &rowFirst, &columnFirst);

printf("Enter rows and column for second matrix:


");
scanf("%d %d", &rowSecond, &columnSecond);

// If colum of first matrix in not equal to row of


second matrix, asking user to enter the size of matrix
again.
while (columnFirst != rowSecond)
{
printf("Error! column of first matrix not equal
to row of second.\n");
printf("Enter rows and column for first matrix:
");
scanf("%d%d", &rowFirst, &columnFirst);
printf("Enter rows and column for second
matrix: ");
scanf("%d%d", &rowSecond,
&columnSecond);
}

// Function to take matrices data


enterData(firstMatrix, secondMatrix, rowFirst,
columnFirst, rowSecond, columnSecond);

// Function to multiply two matrices.


multiplyMatrices(firstMatrix, secondMatrix, mult,
rowFirst, columnFirst, rowSecond, columnSecond);

// Function to display resultant matrix after


multiplication.
display(mult, rowFirst, columnSecond);

return 0;
}

void enterData(int firstMatrix[][10], int


secondMatrix[][10], int rowFirst, int columnFirst, int
rowSecond, int columnSecond)
{
int i, j;
printf("\nEnter elements of matrix 1:\n");
for(i = 0; i < rowFirst; ++i)
{
for(j = 0; j < columnFirst; ++j)
{
printf("Enter elements a%d%d: ", i + 1, j
+ 1);
scanf("%d", &firstMatrix[i][j]);
}
}

printf("\nEnter elements of matrix 2:\n");


for(i = 0; i < rowSecond; ++i)
{
for(j = 0; j < columnSecond; ++j)
{
printf("Enter elements b%d%d: ", i + 1, j
+ 1);
scanf("%d", &secondMatrix[i][j]);
}
}
}

void multiplyMatrices(int firstMatrix[][10], int


secondMatrix[][10], int mult[][10], int rowFirst, int
columnFirst, int rowSecond, int columnSecond)
{
int i, j, k;

// Initializing elements of matrix mult to 0.


for(i = 0; i < rowFirst; ++i)
{
for(j = 0; j < columnSecond; ++j)
{
mult[i][j] = 0;
}
}

// Multiplying matrix firstMatrix and secondMatrix


and storing in array mult.
for(i = 0; i < rowFirst; ++i)
{
for(j = 0; j < columnSecond; ++j)
{
for(k=0; k<columnFirst; ++k)
{
mult[i][j] += firstMatrix[i][k] *
secondMatrix[k][j];
}
}
}
}

void display(int mult[][10], int rowFirst, int


columnSecond)
{
int i, j;
printf("\nOutput Matrix:\n");
for(i = 0; i < rowFirst; ++i)
{
for(j = 0; j < columnSecond; ++j)
{
printf("%d ", mult[i][j]);
if(j == columnSecond - 1)
printf("\n\n");
}
}
}

Output:

Enter rows and column for first matrix: 3


2
Enter rows and column for second matrix: 3
2
Error! column of first matrix not equal to row of
second.

Enter rows and column for first matrix: 2


3
Enter rows and column for second matrix: 3
2

Enter elements of matrix 1:


Enter elements a11: 3
Enter elements a12: -2
Enter elements a13: 5
Enter elements a21: 3
Enter elements a22: 0
Enter elements a23: 4

Enter elements of matrix 2:


Enter elements b11: 2
Enter elements b12: 3
Enter elements b21: -9
Enter elements b22: 0
Enter elements b31: 0
Enter elements b32: 4
Output Matrix:
24 29

6 25
__________________________________________
_________________________
//14.C Program to find the sum of all elements in an
array.
#include <stdio.h>

void main()
{
int a[100];
int i, n, sum=0;

printf("\n\nFind sum of all elements of array:\n");


printf("--------------------------------------\n");

printf("Input the number of elements to be


stored in the array :");
scanf("%d",&n);

printf("Input %d elements in the array :\n",n);


for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&a[i]);
}

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


{
sum += a[i];
}
printf("Sum of all elements stored in the array is :
%d\n\n", sum);
}

Output:
Find sum of all elements of array:
--------------------------------------
Input the number of elements to be stored in the
array :3
Input 3 elements in the array :
element - 0 : 2
element - 1 : 5
element - 2 : 8
Sum of all elements stored in the array is : 15
__________________________________________
___________________________
//15.C program to read a sorted list of floating point
values then calculate and display
the median of the values.
#include<stdio.h>
#define N 10
main( ){
int i,j,n;
float median,a[N],t;
printf("Enter the number of items
");
scanf("%d", &n);
/* Reading items into array a */
printf("Input %d values
",n);
for (i = 1; i <= n ; i++)
scanf("%f", &a[i]);
/* Sorting begins */
for (i = 1 ; i <= n-1 ; i++){ /* Trip-i begins */
for (j = 1 ; j <= n-i ; j++) {
if (a[j] <= a[j+1]) { /* Interchanging values */
t = a[j];
a[j] = a[j+1];
a[j+1] = t;
}
else
continue ;
}
} /* sorting ends */
/* calculation of median */
if ( n % 2 == 0)
median = (a[n/2] + a[n/2+1])/2.0 ;
else
median = a[n/2 + 1];
/* Printing */
for (i = 1 ; i <= n ; i++)
printf("%f ", a[i]);
printf("Median is %f", median);
}
Output:
Enter the number of items
5
Input 5 values
2.3
1.2
3.8
4.6
8.9
8.900000 4.600000 3.800000 2.300000 1.200000

Median is 3.800000
__________________________________________
___________________
//16.C Program to sort the given array using bubble
sort.
#include <stdio.h>
void bubble_sort(int arr[], int n) {
int i, j;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
bubble_sort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}

Output:
Sorted array: 11 12 22 25 34 64 90
__________________________________________
___________________
//17.C Program to copy the contents of one array
into another in the reverse order.
// C program to copy the contents
// of one array into another
// in the reverse order

#include <stdio.h>
// Function to print the array
void printArray(int arr[], int len)
{
int i;
for (i = 0; i < len; i++) {
printf("%d ", arr[i]);
}
}

// Driver code
int main()
{
int original_arr[] = {1, 2, 3, 4, 5};
int len = sizeof(original_arr)/sizeof(original_arr[0]);

int copied_arr[len], i, j;
// Copy the elements of the array
// in the copied_arr in Reverse Order
for (i = 0; i < len; i++) {
copied_arr[i] = original_arr[len - i - 1];
}

// Print the original_arr


printf("\nOriginal array: ");
printArray(original_arr, len);

// Print the copied array


printf("\nResultant array: ");
printArray(copied_arr, len);

return 0;
}

Output:
Original array: 1 2 3 4 5
Resultant array: 5 4 3 2 1
__________________________________________
______________________
//18.Twenty five numbers are entered from the
keyboard into an array. Write a C
Program to find out how many of them are prime
numbers.
#include <stdio.h>

//function to check number is prime or not


//function will return 1 if number is prime
int isPrime(int num)
{
int i; //loop counter
//it will be 1 when number is not prime
int flag=0;
//loop to check number is prime or not
//we will check, if number is divisible
//by any number from 2 to num/2, then it
//will not be prime
for(i=2; i<num/2; i++)
{
if(num%i ==0)
{
flag =1;
break;
}
}
//flag is 1, if number is not prime
if(flag==1)
return 0;
else
return 1;
}
int main()
{
int loop,a[10]; //loop counter
printf("Enter the number of elements of an array one
by one:\n");
for(i=0;i<n;i++)
{
scanf(%d%d,&a[i]);
}
//declaring array with prime and not prime
numbers
//int arr[]={100, 200, 31, 13, 97, 10, 20, 11};
//calculate length of the array
int len = sizeof(arr)/sizeof(arr[0]);

//print array elements with message


//"prime" or "Not prime"
for(loop=0; loop<len; loop++)
{
printf("%3d -
%s\n",arr[loop],(isPrime(arr[loop])?"Prime":"Not
Prime"));
}

printf("\n");

return 0;
}

Output:
100 - Not Prime
200 - Not Prime
31 - Prime
13 - Prime
97 - Prime
10 - Not Prime
20 - Not Prime
11 - Prime
__________________________________________
______________________
//19.C program to find whether the given element is
present in an array or not
using Linear and Binary search.
#include <stdio.h>
int main()
{
int array[100], search, c, number;
printf("Enter the number of elements in array\n");
scanf("%d",&number);
printf("Enter %d numbers\n", number);
for ( c = 0 ; c < number ; c++ )
scanf("%d",&array[c]);
printf("Enter the number to search\n");
scanf("%d",&search);
for ( c = 0 ; c < number ; c++ )
{
if ( array[c] == search ) /* if required element
found */
{
printf("%d is present at location %d.\n",
search, c+1);
break;
}
}
if ( c == number )
printf("%d is not present in array.\n", search);
return 0;
}

Output:
Enter the number of elements in array
5
Enter 5 numbers
12
23
22
10
45
Enter the number to search
22
22 is present at location 3.

//Binary Search
//Program Name: BinarySearch.c #include<stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d",&array[c]);
printf("Enter value to find\n");
scanf("%d",&search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while( first <= last )
{
if ( array[middle] < search )
first = middle + 1;
else if ( array[middle] == search )
{
printf("%d found at location %d.\n", search,
middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if ( first > last )
printf("Not found! %d is not present in the
list.\n", search);
return 0;
}

Output:
Enter number of elements
6
Enter 6 integers
10
30
20
15
56
100
Enter value to find
33
Not found! 33 is not present in the list.
__________________________________________
_______________________
//20.C program to insert an element in a sorted
array.
#include <stdio.h>

int main()
{
int arr1[100],i,n,p,inval;
printf("\n\nInsert New value in the sorted array
:\n");
printf("-----------------------------------------\n");
printf("Input the size of array : ");
scanf("%d", &n);
/* Stored values into the array*/
printf("Input %d elements in the array in
ascending order:\n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}
printf("Input the value to be inserted : ");
scanf("%d",&inval);
printf("The exist array list is :\n");
for(i=0;i<n;i++)
printf("% 5d",arr1[i]);
/* Determine the position where the new value will
be insert.*/
for(i=0;i<n;i++)
{
if(inval<arr1[i])
{
p = i;
break;
}
else
{
p=i+1;
}
}
/* move all data at right side of the array */
for(i=n+1;i>=p;i--)
arr1[i]= arr1[i-1];
/* insert value at the proper position */
arr1[p]=inval;
printf("\n\nAfter Insert the list is :\n");
for(i=0;i<=n;i++)
printf("% 5d",arr1[i]);
printf("\n");
}

Output:
Insert New value in the sorted array :
-----------------------------------------
Input the size of array : 6
Input 6 elements in the array in ascending order:
element - 0 : 2
element - 1 : 5
element - 2 : 7
element - 3 : 11
element - 4 : 9
element - 5 : 6
Input the value to be inserted : 8
The exist array list is :
2 5 7 11 9 6
After Insert the list is :
2 5 7 8 11 9 6

--------------------------------
Process exited after 33.18 seconds with return value
10
Press any key to continue . . .
__________________________________________
_____________________________
//21.C program to arrange the values of an array in
such a way that even numbers
precede the odd numbers.
#include <conio.h>

int main()
{
int a[10000],b[10000],i,n,j,k,temp,c=0;
printf("Enter size of the array : ");
scanf("%d", &n);
printf("Enter elements in array : ");
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
if(a[i]%2==1)
c++;
}
for(i=0; i<n-1; i++)
{

for(j=0; j<n-i-1; j++)


{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}

k=0;
j=n-c;

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


{
if(a[i]%2==0)
{
if(k<n-c)
b[k++]=a[i];
}
else
{
if(j<n)
b[j++]=a[i];
}
}

printf("\narray after sorting even and odd elements


separately:\n ");

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


{
a[i]=b[i];
printf("%d ",a[i]);
}

}
Output:
Enter size of the array : 10
Enter elements in array : 0 1 2 3 4 5 6 7 8 9

array after sorting even and odd elements


separately:
0246813579
__________________________________________
______________________
//22.Fill an array with positive and negative integers.
Write a C program to separate
positive and negative numbers into two sub-arrays
called “positive” and “negative”
and perform the following activities:
Add two arrays
Find the maximum and minimum of both arrays
Find the size of both arrays and print the array
name which has more elements
__________________________________________
_____________________________
//24.C program to find the number of non repeated
numbers in an array and print
thenumbers and their count (ex:12233345…. Ans:
count is 3, and the numbers are
1 4 5).
#include <stdio.h>

// Main function to run the program


int main()
{
int arr[] = {21, 30, 10, 2, 10, 20, 30, 11};
int n = sizeof(arr)/sizeof(arr[0]);

int i,j,visited[n];

for(i=0; i<n; i++){


if(visited[i]==0){
int count = 1;
for(j=i+1; j<n; j++){
if(arr[i]==arr[j]){
count++;
visited[j]=1;
}
}
if(count==1)
printf("%d ",arr[i]);
}
}

return 0;
}
Output:
2 20 11
--------------------------------
__________________________________________
____________________________
//25.C program to find the sum of diagonal elements
in a given matrix.

// C Program to demonstrate the


// Sum of Diagonals of a Matrix
#include <stdio.h>

int main()
{

int i, j, m = 3, n = 3, a = 0, sum = 0;

// input matrix
int matrix[3][3]
= { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };

// if both rows and columns are equal then it is


// possible to calculate diagonal sum
if (m == n) {

// printing the input matrix


printf("The matrix is \n");

// iterates number of rows


for (i = 0; i < m; ++i) {

// iterates number of columns


for (j = 0; j < n; ++j) {
printf(" %d", matrix[i][j]);
}
printf("\n");
}
for (i = 0; i < m; ++i) {

// calculating the main diagonal sum


sum = sum + matrix[i][i];

// calculating the off diagonal sum


a = a + matrix[i][m - i - 1];
}

// printing the result


printf("\nMain diagonal elements sum is =
%d\n", sum);
printf("Off-diagonal elements sum is =
%d\n", a);
}
else
// if both rows and columns are not equal
then it is
// not possible to calculate the sum
printf("not a square matrix\n");
return 0;
}

Output:
The matrix is
123
456
789

Main diagonal elements sum is = 15


Off-diagonal elements sum is = 15
//26.C program to check whether the given matrix is
symmetric or skew Symmetric.
#include <stdio.h>

int main() {
int A[3][3], B[3][3];
int row, col, isSym;

// Take a matrix A as input from user


printf("Enter the elements in matrix of size 3x3: \n");
for (row = 0; row < 3; row++) {
for (col = 0; col < 3; col++) {
scanf("%d", &A[row][col]);
}
}

// Finds the transpose of matrix A


for (row = 0; row < 3; row++) {
for (col = 0; col < 3; col++) {
// Stores each row of matrix A to each column of
matrix B
B[row][col] = A[col][row];
}
}

// Checks whether matrix A is equal to its transpose


or not
isSym = 1;
for (row = 0; row < 3 && isSym; row++) {
for (col = 0; col < 3; col++) {
if (A[row][col] != B[row][col]) {
isSym = 0;
break;
}
}
}
// If the given matrix is symmetric.
if (isSym == 1) {
printf("\n Matrix is Symmetric. \n");

for (row = 0; row < 3; row++) {


for (col = 0; col < 3; col++) {
printf("%d ", A[row][col]);
}

printf("\n");
}
} else {
printf("\n Matrix is not Symmetric.");
}

return 0;
}
Output:
Enter the elements in matrix of size 3x3:
2
3
6
3
4
5
6
5
9

Matrix is Symmetric.
236
345
659
//27.program to find the sum of upper triangular and
lower triangular of the square
matrix.

Output:

//28.Menu driven C program to add, subtract and


multiply the contents of two given
matrices.

Output:
//29.C program to count the total number of non-zero
elements in a twodimensionalarray.

Output:

You might also like