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

Convert the following expression to postfix and prefix (A+B) * (D-C)

3 Answers   Satyam,


what is the difference between <stdio.h> and "stdio.h"

14 Answers   Invendis, Kanbay, Mastek, MathWorks,


will u give me old quesrion papers for aptitude for L & t info tech?

1 Answers   Hindustan, L&T,


write a program for egyptian fractions in c?

1 Answers   Satyam,


Explain indirection?

0 Answers  






7-Given an index k, return the kth row of the Pascal's triangle. For example, when k = 3, the row is [1,3,3,1]. For reference look at the following standard pascal’s triangle.

0 Answers  


What is c standard library?

0 Answers  


Why is c so important?

0 Answers  


how to create duplicate link list using C???

0 Answers  


What is header file definition?

0 Answers  


What is operator precedence?

0 Answers  


i want to switch my career from quailty assurance engineering to development kindly guide me from which programming language its better for me to start plz refer some courses or certifications too i have an experience of 1.5 yrs in QA field.Kindly guide me

0 Answers   Microsoft,


Categories