You are on page 1of 5

Prev Page Next Page

Java Program to Display Upper Triangular Matrix

This is a Java Program to Display Upper Triangular Matrix.


Enter the elements of array as input. we use loops and if-else conditions to print only the diagonals and the elements above diagonal will be
printed as it is and rest of the elements as zeros.
Here is the source code of the Java Program to Display Upper Triangular Matrix. The Java program is successfully compiled and run on a
Windows system. The program output is also shown below.

converted by W eb2PDFConvert.com
1. import java.util.Scanner;
2. public class Upper_Triangular
3. {
4. public static void main(String args[])
5. {
6. int a[][] = new int[5][5];
7. System.out.println("Enter the order of your Matrics ");
8. System.out.println("Enter the rows:");
9. Scanner sc = new Scanner(System.in);
10. int n = sc.nextInt();
11. System.out.println("Enter the columns:");
12. Scanner s = new Scanner(System.in);
13. int m = s.nextInt();
14.
15. if(n == m)
16. {
17. System.out.println("Enter your elements:");
18. for(int i = 0; i < n; i++)
19. {
20. for(int j = 0; j < n; j++)
21. {
22. Scanner ss = new Scanner(System.in);
23. a[i][j] = ss.nextInt();
24. System.out.print(" ");
25. }
26. }
27. System.out.println("You have entered:");
28. for(int i = 0; i < n; i++)
29. {
30. for(int j = 0; j < n; j++)
31. {
32. System.out.print(a[i][j] + " ");
33. }
34. System.out.println("");
35. }
36. System.out.println("The Upper Triangular Matrices is:");
37. for(int i = 0; i < n; i++)
38. {
39. for(int j = 0; j < n; j++)
40. {
41. if(i <= j)
42. {
43. System.out.print(a[i][j] +" ");
44. }
45. else
46. {
47. System.out.print("0"+" ");
48. }
49. }
50. System.out.println("");
51. }
52. }
53. else
54. {
55. System.out.println("you have entered improper order");
56. }
57. }
58. }
59. }

Output:

converted by W eb2PDFConvert.com
$ javac Upper_Triangular.java
$ java Upper_Triangular

Enter the order of your Matrics


Enter the rows:
3
Enter the columns:
3
Enter your elements:
1
2
3
4
5
6
7
8
9
You have entered:
1 2 3
4 5 6
7 8 9
The Upper Triangular Matrices is:
1 2 3
0 5 6
0 0 9

Sanfoundry Global Education & Learning Series 1000 Java Programs.


Heres the list of Best Reference Books in Java Programming, Data Structures and Algorithms.
Prev Page - Java Program to Display Lower Triangular Matrix
Next Page - Java Program to Display Transpose Matrix

Deep Dive @ Sanfoundry:


1. Java Programming Examples on Computational Geometry Problems & Algorithms
2. Java Algorithms, Problems & Programming Examples
3. Java Training I Java Foundation Training
4. Java Programming Examples on Graph Problems & Algorithms
5. Java Programming Examples on Collection API
6. Java Programming Examples on Combinatorial Problems & Algorithms
7. Java Programming Examples on Data-Structures
8. Java Programming Examples on Numerical Problems & Algorithms
9. C# Programming Examples on Matrix
10. C Programming Examples on Matrix

converted by W eb2PDFConvert.com
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He
is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas.
He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux
Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C
Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel.
Stay connected with him below:
LinkedIn | Facebook | Twitter | Google+

Subscribe Sanfoundry Newsletter and Posts

Name*

Email*

Best Careers

Developer Tracks
SAN Developer
Linux Kernel Developer
Linux Driver Developer
Linux Network Developer

Live Training Photos


Mentoring
Software Productivity
GDB Assignment

Sanfoundry is No. 1 choice for Deep Hands-ON Trainings in SAN, Linux & C, Kernel Programming. Our Founder has trained employees of
almost all Top Companies in India such as VMware, Citrix, Oracle, Motorola, Ericsson, Aricent, HP, Intuit, Microsoft, Cisco, SAP Labs,
Siemens, Symantec, Redhat, Chelsio, Cavium, ST-Micro, Samsung, LG-Soft, Wipro, TCS, HCL, IBM, Accenture, HSBC, Mphasis, Tata-Elxsi,
Tata VSNL, Mindtree, Cognizant and Startups.

Best Trainings

SAN I - Technology
SAN II - Admin
Linux Fundamentals
Advanced C Training
Linux-C Debugging
System Programming
Network Programming
Linux Threads
Kernel Programming
Kernel Debugging
Linux Device Drivers

Best Reference Books

Computer Science Books


Algorithm & Programming Books
Electronics Engineering Books

converted by W eb2PDFConvert.com
Electrical Engineering Books
Chemical Engineering Books
Civil Engineering Books
Mechanical Engineering Books
Industrial Engineering Books
Instrumentation Engg Books
Metallurgical Engineering Books
All Stream Best Books

Questions and Answers

1000 C Questions & Answers


1000 C++ Questions & Answers
1000 C# Questions & Answers
1000 Java Questions & Answers
1000 Linux Questions & Answers
1000 Python Questions
1000 PHP Questions & Answers
1000 Hadoop Questions
Cloud Computing Questions
Computer Science Questions
All Stream Questions & Answers

India Internships

Computer Science Internships


Instrumentation Internships
Electronics Internships
Electrical Internships
Mechanical Internships
Industrial Internships
Systems Internships
Chemical Internships
Civil Internships
IT Internships
All Stream Internships

About Sanfoundry

About Us
Copyright
TOS & Privacy
Jobs
Bangalore Training
Online Training
SAN Training
Developers Track
Mentoring Sessions
Contact Us
Sitemap
2011-2017 Sanfoundry

converted by W eb2PDFConvert.com

You might also like