 # C Programming Code Examples

## C > Arrays and Matrices Code Examples

### program to find sum of main diagonal elements of a matrix

``` program to find sum of main diagonal elements of a matrix Write a C program to read elements in a matrix and find the sum of main diagonal (major diagonal) elements of matrix. Find sum of all elements of main diagonal of a matrix. #include <stdio.h> #define SIZE 3 // Matrix size int main() { int A[SIZE][SIZE]; int row, col, sum = 0; /* Input elements in matrix from user */ printf("Enter elements in matrix of size %dx%d: \n", SIZE, SIZE); for(row=0; row<SIZE; row++) { for(col=0; col<SIZE; col++) { scanf("%d", &A[row][col]); } } /* Find sum of main diagonal elements */ for(row=0; row<SIZE; row++) { sum = sum + A[row][row]; } printf("\nSum of main diagonal elements = %d", sum); return 0; } ```

Code Converts Binary Number to Decimal - C language code to convert binary to decimal number system. The Binary number system is base 2 number system & uses only 2 symbols to represent all its numbers i.e. 0 and 1. Input

C Code Remove Spaces Blanks from String - C program remove extra spaces, blanks from the string. Removes 'extra blank spaces' from the given string and returns a new string with single "blank spaces" and skip all consecutive

C Convert a Time value to and from UTC - Status epilepticus, print help. Extract UTC time from string. Convert UTC to time string. Print help and exit. Convert INT utc value to timestring. Convert STR to INT utc time...

C++ Code Calculate Power of any Number - For calculating the power of any number user have four possibilities to input values. Sample code only work for when we input +ve power, +ve 'base value', -ve base value. It is not work

C Code Reverse the Stack using Recursion - C program code, 'using recursion', reverses a Stack content. Stack here is represented use a linked list. A Linked List is an ordered set of "Data Elements", each containing a link to its

Finds First Occurence of the any Character - Program takes 2 strings as input and finds the first occurence of the any character of String2 in String1 & also its position. Take two strings as input. Compare both the strings using two

Swapping Two Numbers Without Variable - Imagine we have two number 1 and 2 stored in x and j respectively now. If we add x and j (1 + 2) and store it to x then x will become 3 and j is still 2. We subtract j (2) from new value of

Remove all Occurrences of Word in String - C program remove all 'occurrences' of a given word in string using loop. Logic to remove all occurrences of a word in a string. Input string from user, store it in some variable say string.