Write a C Program to print lower diagonal of a matrix.
Question
Write a C Program to print lower diagonal of a matrix.
Solution
Sure, here is a simple C program that prints the lower diagonal of a matrix:
#include<stdio.h>
int main() {
int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int i, j;
printf("Lower Diagonal Elements: ");
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++) {
if(j <= i) {
printf("%d ", matrix[i][j]);
}
}
}
return 0;
}
This program first defines a 3x3 matrix. Then it uses two nested loops to iterate over the elements of the matrix. If the column index j is less than or equal to the row index i, it prints the element at that position. This is because in the lower diagonal of a matrix, the row index is always greater than or equal to the column index.
Similar Questions
Write a C Program to Compute the Sum of Diagonals of a square matrix. Print the sum of both diagonals separately. Get the number of rows and columns from the user.
Write a C program that calculates the sum of both diagonals in a square matrix. Implement a function that takes a square matrix and its size as input and returns the sum of the main diagonal and anti-diagonal elements.
Write a python program that defines a matrix and prints
Let A be a square matrix of size n x n. Consider the following program. What is the expected output? #include <stdio.h>int main() { int A[2][2]={1,2,3,4},i,j,C=100,Temp,n=2; for(i=0;i<n;i++) for(j=0;j<n;j++){ Temp = A[i][j] + C; A[i][j] = A[j][i]; A[j][i] = Temp - C; } for(i=0;i<n;i++){ for(j=0;j<n;j++) printf("%d ",A[i][j]); printf("\n"); } return 0;}
What will be the output of the following C code? #include void main() { int a[2][3] = {1, 2, 3, 4, 5}; int i = 0, j = 0; for (i = 0; i < 2; i++) for (j = 0; j < 3; j++) printf("%d", a[i][j]); }a.1 2 3 4 5 0b.1 2 3 4 5 junkc.1 2 3 4 5 5d.Run time errorClear my choice
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.