Introduction
The diagonal elements of a matrix are the elements that lie on the line extending from the top left corner to the bottom right corner (known as the main diagonal) and optionally, from the top right corner to the bottom left corner (known as the secondary diagonal). This guide will show you how to write a C program that prints the diagonal elements of a square matrix.
Problem Statement
Create a C program that:
- Takes a square matrix as input from the user.
- Prints the elements on the main diagonal of the matrix.
- Optionally prints the elements on the secondary diagonal.
Example:
- Input:
- Matrix:
1 2 3 4 5 6 7 8 9
- Matrix:
- Output:
- Main Diagonal:
1 5 9
- Secondary Diagonal:
3 5 7
- Main Diagonal:
Solution Steps
- Include the Standard Input-Output Library: Use
#include <stdio.h>
for standard input-output functions. - Write the Main Function: Define the
main
function, which is the entry point of every C program. - Declare Variables: Declare variables to store the matrix, its dimension, and loop counters.
- Input the Dimension and Elements of the Matrix: Use loops to take input from the user for the matrix.
- Print the Main Diagonal Elements: Use a loop to print the elements on the main diagonal.
- Optionally Print the Secondary Diagonal Elements: Use a loop to print the elements on the secondary diagonal.
- Display the Result: Use
printf
to display the diagonal elements.
C Program to Print the Diagonal Elements of a Matrix
#include <stdio.h> int main() { // Step 1: Declare variables to hold the matrix and its dimension int n; int matrix[100][100]; int i; // Step 2: Prompt the user to enter the dimension of the square matrix printf("Enter the dimension of the square matrix (n x n): "); scanf("%d", &n); // Step 3: Input the elements of the matrix printf("Enter elements of the matrix:\n"); for (i = 0; i < n; i++) { for (int j = 0; j < n; j++) { scanf("%d", &matrix[i][j]); } } // Step 4: Print the main diagonal elements printf("Main Diagonal: "); for (i = 0; i < n; i++) { printf("%d ", matrix[i][i]); } printf("\n"); // Step 5: Print the secondary diagonal elements printf("Secondary Diagonal: "); for (i = 0; i < n; i++) { printf("%d ", matrix[i][n - i - 1]); } printf("\n"); return 0; // Step 6: Return 0 to indicate successful execution }
Explanation
Step 1: Declare Variables
- The variable
n
stores the dimension of the square matrix. Thematrix
array stores the elements of the matrix. The variablei
is used as a loop counter.
Step 2: Input the Dimension of the Matrix
- The program prompts the user to enter the dimension (
n x n
) of the square matrix usingscanf
.
Step 3: Input the Elements of the Matrix
- The program uses nested
for
loops to take input for each element of the matrix from the user.
Step 4: Print the Main Diagonal Elements
- The program uses a
for
loop to iterate through the matrix and print the elements on the main diagonal:- The main diagonal elements are those where the row index is equal to the column index, i.e.,
matrix[i][i]
.
- The main diagonal elements are those where the row index is equal to the column index, i.e.,
Step 5: Print the Secondary Diagonal Elements
- The program uses another
for
loop to iterate through the matrix and print the elements on the secondary diagonal:- The secondary diagonal elements are those where the row index plus the column index equals
n-1
, i.e.,matrix[i][n - i - 1]
.
- The secondary diagonal elements are those where the row index plus the column index equals
Step 6: Return 0
- The
return 0;
statement indicates that the program executed successfully.
Output Example
Example 1:
Enter the dimension of the square matrix (n x n): 3 Enter elements of the matrix: 1 2 3 4 5 6 7 8 9 Main Diagonal: 1 5 9 Secondary Diagonal: 3 5 7
Example 2:
Enter the dimension of the square matrix (n x n): 4 Enter elements of the matrix: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Main Diagonal: 1 6 11 16 Secondary Diagonal: 4 7 10 13
Conclusion
This C program demonstrates how to print the diagonal elements of a square matrix, covering both the main and secondary diagonals. It covers basic concepts such as arrays, loops, and matrix operations, making it a useful example for beginners learning C programming.