loader image

C program to fill upper triangle with 1, lower triangle with -1 and diagonal elements with 0

C program to fill upper triangle with 1, the lower triangle with -1 and diagonal elements with 0.

#include <stdio.h>
int main()
{
	int a[10][10], i, j, m, n;
	printf("Enter the order : ");
	scanf("%d%d", &m, &n);
	for (i = 0; i < m; i++)
	{
		for (j = 0; j < n; j++)
		{
			if (i < j)
				a[i][j] = 1;
			else if (i > j)
				a[i][j] = -1;
			else
				a[i][j] = 0;
		}
	}
	printf("\nThe matrix :\n");
	for (i = 0; i < m; i++)
	{
		printf("\n");
		for (j = 0; j < n; j++)
			printf("%4d", a[i][j]);
	}
}
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Scroll to Top