program to print upper & lower triangle of a matrix
Answers were Sorted based on User's Feedback
Answer / medhane
#include (iostream.h)
#include (conio.h)
# define n 10
void main( )
{
int mat[n][n];
int d;
// Input elements
cout<< "\n Enter dimension of square matrix:";
cin >> d;
for(int i = 0; i < d ; i++)
for( int j = 0; j < d ; j++)
{cout<<"\n Enter elements for "<< i+1 << "," << j+1
<"location :";
cin >> mat[i][j];
}
clrscr();
//Print the array
cout<<"\n The Original matrix : \n\n";
for( i = 0; i < d ; i++)
{for( j = 0; j < d ; j++)
cout<< mat[i][j]<<"\t";
cout<< "\n";
}
//upper half of left diagonal ----
cout<<"\n The Upper half of the matrix : \n\n";
for( i = 0; i < d ; i++)
{
for( j = 0; j < d ; j++)
{ if(i < j)
cout << mat [i][j] << " " ;
else
cout << " " << " ";
}
cout << "\n ";
}
//lower half of left diagonal -----
cout<<"\n The Lower half of the matrix : \n\n";
for( i = 0; i < d ; i++)
{
for( j = 0; j < d ; j++)
{ if(i > j)
cout << mat [i][j] << " " ;
else
cout << " " << " ";
}
cout << "\n ";
}
getch ( );
}
Is This Answer Correct ? | 16 Yes | 14 No |
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[5][5],i,j;
clrscr();
printf("\n");
//upper triangle matrix
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
arr[i][j]=(i+j);
}
}
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(j<4-i)
{printf(" ");
}
else printf("%d",arr[i][j]);
}
printf("\n");
}
printf("\n");
//lower triangle matrix
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(j>=4-i)
{printf(" ");
}
else printf("%d",arr[i][j]);
}
printf("\n");
}
getch();
}
Is This Answer Correct ? | 6 Yes | 9 No |
Explain 'bit masking'?
what is the need for main function in c?
print the pattern 1 2 4 3 6 9 4 8 12 16 5 10 15 20 25 if n=5
How do we select the big element or any other operation from array which is read dynamically. user need to give the elements only no need to mention the size.
What are the advantages of using linked list for tree construction?
What is scanf_s in c?
for (i <= 5 && i >= -1;++i; i > 0) { printf("%d ", i); }
how to find anagram without using string functions using only loops in c programming
Write a program to replace n bits from the position p of the bit representation of an inputted character x with the one's complement. Method invertBit takes 3 parameters x as input character, p as position and n as the number of positions from p. Replace n bits from pth position in 8 bit character x. Then return the characters by inverting the bits.
how to add two numbers without using arithmetic operators?
How can I recover the file name given an open stream or file descriptor?
What is #include cctype?