Print Triangular Array

From Java Example Source Code

Jump to: navigation, search

Contents

[edit] Overview - Print Triangular Array

This Java example program shows how to print a triangular [[Category:Array|array].

[edit] Java Source Code

  • Package: example.array
  • File: TriangularArray.java
  1. package example.array;
  2.  
  3.  
  4. public class TriangularArray {
  5.     public static void main(String[] args) {
  6. 	final int MAX_HIGH = 10;
  7.  
  8. 	// allocate triangular array
  9. 	int[][] odds = new int[MAX_HIGH + 1][];
  10. 	for (int i = 0; i <= MAX_HIGH; i++)
  11. 	    odds[i] = new int[i + 1];
  12.  
  13. 	// fill triangular array
  14. 	for (int i = 0; i < odds.length; i++)
  15. 	    for (int j = 0; j < odds[i].length; j++)
  16. 		odds[i][j] = i + j;
  17.  
  18. 	// print triangular array
  19. 	for (int i = 0; i < odds.length; i++) {
  20. 	    for (int j = 0; j < odds[i].length; j++) {
  21. 		System.out.print(odds[i][j] + " ");
  22. 	    }
  23. 	    System.out.println();
  24.  
  25. 	}
  26.     }
  27. }

[edit] What Result You Can Get

Run the program, you will get:

0 
1 2 
2 3 4 
3 4 5 6 
4 5 6 7 8 
5 6 7 8 9 10 
6 7 8 9 10 11 12 
7 8 9 10 11 12 13 14 
8 9 10 11 12 13 14 15 16 
9 10 11 12 13 14 15 16 17 18 
10 11 12 13 14 15 16 17 18 19 20 

[edit] Required External Libraries and/or Files for this Java Example

Need nothing.

http://www.ase2009.com/ online casino 132 http://www.hellzyea.com/health health insurance 8DDD http://www.yourautoinsurancesite.com/ auto insurance quotes 135811 http://www.makemeasammich.com/ auto insurance mcnxu

[edit] Question & Answer

Any question?

Click edit and post your question or answer here.


Personal tools