Showing posts with label Upper triangular matrix. Show all posts
Showing posts with label Upper triangular matrix. Show all posts

Thursday, 12 March 2015

Cpp Program to display Upper Triangular form of a matrix.



This is a Cpp program which display the upper triangular form of a given matrix.

To display the upper triangular form of a given matrix we make all the elements below the diagonal row as zero.





See the figure below,
Upper Triangular form of a given matrix




Download Code
PROGRAM :
#include <iostream>

using namespace std;

int main()
{
    int rows,columns,i,j;
    cout << "Enter order of matrix  \t:";
    cin >> rows;
    cin >> columns;
    int matrix[rows][columns];
    cout << "\n\nEnter elements of a matrix\n";
    for(i=0; i<rows; i++)
    {
        for(j=0; j<columns; j++)
        {
            cout << "Enter element m" << i+1 << j+1 << ": \t";
            cin >> matrix[i][j];
        }
    }
    cout << "\n\nMATRIX is :\n";
    for(i=0; i<rows; i++)
    {
        for(j=0; j<columns; j++)
        {
            cout << matrix[i][j] << "\t";
        }
        cout << endl;
    }
    cout << "\n\nIts Upper Triangular form is: \n";
    for(i=0; i<rows; i++)
    {
        for(j=0; j<columns; j++)
        {
            if(i<=j)
                cout << matrix[i][j] << "\t";
            else
                cout << 0 << "\t";
        }
        cout << endl;
    }
    return 0;
}


OUTPUT : 
Upper Triangular form of a given matrix


Download Code

Monday, 12 January 2015

Java Pogram to find the upper triangular form for a given matrix.



This is a Java program which display the upper triangular form of a given matrix.

To display the upper triangular form of a given matrix we make all the elements below the diagonal row as zero.




See the figure below,


PROGRAM  : 
package codingcorner.in;

import java.util.Scanner;

public class UpperTriangularMatrix {
 public static void main(String[] args) {
  int rows, columns, i, j;
  Scanner scan = new Scanner(System.in);
  System.out.print("Enter order of matrix :  \t");
  rows = scan.nextInt();
  columns = scan.nextInt();
  int matrix[][] = new int[rows][columns];
  System.out.print("Enter elements of the matrix :\n");
  for (i = 0; i < rows; i++) {
   for (j = 0; j < columns; j++) {
    System.out.print("Enter element m" + (i + 1) + (j + 1) + ":\t");
    matrix[i][j] = scan.nextInt();
   }
  }
  scan.close();
  System.out.print("\n\nMATRIX is :\n");
  for (i = 0; i < rows; i++) {
   for (j = 0; j < columns; j++)
    System.out.print(matrix[i][j] + "\t");

   System.out.print("\n");
  }

  System.out.print("\n\nIts Upper Triangular form is: \n");
  for (i = 0; i < rows; i++) {
   for (j = 0; j < columns; j++) {
    if (i <= j)
     System.out.print(matrix[i][j] + "\t");
    else
     System.out.print(0 + "\t");
   }
   System.out.print("\n");
  }

 }
}

OUTPUT : 
Java -  Upper Triangular Form

Saturday, 10 January 2015

C Program to display Upper Triangular Matrix



This is a C program which display the upper triangular form of a given matrix.

To display the upper triangular form of a given matrix we make all the elements below the diagonal row as zero.




See the figure below,
PROGRAM :
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int rows,columns,i,j;
    printf("Enter order of matrix  \t:");
    scanf("%d%d",&rows,&columns);
    int matrix[rows][columns];
    printf("\n\nEnter elements of a matrix\n");
    for(i=0;i<rows;i++)
    {
        for(j=0;j<columns;j++)
        {
            printf("Enter element m%d%d: ",i+1,j+1);
            scanf("%d",&matrix[i][j]);
        }
    }
    printf("\n\nMATRIX is :\n");
    for(i=0;i<rows;i++)
    {
        for(j=0;j<columns;j++)
        {
            printf("%d\t",matrix[i][j]);
        }
        printf("\n");
    }
    printf("\n\nIts Upper Triangular form is: \n");
    for(i=0;i<rows;i++)
    {
        for(j=0;j<columns;j++)
        {
            if(i<=j)
            printf("%d\t",matrix[i][j]);
            else
                printf("%d\t",0);
        }
        printf("\n");
    }
    return 0;
}

OUTPUT :
C -Upper Triangular MAtrix