program to print upper & lower triangle of a matrix

Answers were Sorted based on User's Feedback



program to print upper & lower triangle of a matrix..

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

program to print upper & lower triangle of a matrix..

Answer / arka bandyopadhyay

#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

Post New Answer

More C Interview Questions

Explain 'bit masking'?

0 Answers   EXL,


what is the need for main function in c?

5 Answers  


print the pattern 1 2 4 3 6 9 4 8 12 16 5 10 15 20 25 if n=5

3 Answers   Winit,


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.

0 Answers  


What are the advantages of using linked list for tree construction?

0 Answers  






What is scanf_s in c?

0 Answers  


for (i <= 5 && i >= -1;++i; i > 0) { printf("%d ", i); }

1 Answers  


how to find anagram without using string functions using only loops in c programming

1 Answers   Mind Tree, TCS,


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.

0 Answers   Infosys,


how to add two numbers without using arithmetic operators?

4 Answers  


How can I recover the file name given an open stream or file descriptor?

0 Answers  


What is #include cctype?

0 Answers  


Categories