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

int n=1; while(1) { switch(n) { case 1:printf("a"); n++; continue; case 2:printf("b"); n++; continue; default : printf("c"); break; } break; }

1 Answers  


Tell me is null always defined as 0(zero)?

0 Answers  


Why c language is called c?

0 Answers  


how many times of error occur in C

11 Answers  


Write a code to reverse string seperated by spaces i/p str=India is my country o/p str=aidnI si ym yrtnuoc After writing code, optimize the code

1 Answers  






Explain the difference between strcpy() and memcpy() function?

0 Answers  


Why c is a procedural language?

0 Answers  


What is the use of a static variable in c?

0 Answers  


What does c mean in standard form?

0 Answers  


When would you use a pointer to a function?

0 Answers  


Want to know how to write a C program that connects to a MySQL server and checks if the InnoDB plug-in is installed on it. If so, your program should print the total number of disk writes by MySQL.

0 Answers  


what is the value of b if a=5; b=++a + ++a

31 Answers   Infosys, TCS, Tech Mahindra,


Categories