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 |
int n=1; while(1) { switch(n) { case 1:printf("a"); n++; continue; case 2:printf("b"); n++; continue; default : printf("c"); break; } break; }
Tell me is null always defined as 0(zero)?
Why c language is called c?
how many times of error occur in C
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
Explain the difference between strcpy() and memcpy() function?
Why c is a procedural language?
What is the use of a static variable in c?
What does c mean in standard form?
When would you use a pointer to a function?
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.
what is the value of b if a=5; b=++a + ++a
31 Answers Infosys, TCS, Tech Mahindra,