Share on Google+Share on Google+

Java create triangle pattern

Advertisement
In this tutorial, you will learn how to create a triangle shape.

Java create triangle pattern

In this tutorial, you will learn how to create a triangle shape. For this, user is allowed to enter number of lines to display the triangle of that length and a symbol to show the triangle in a particular symbol.

Example:

import java.util.*;

 public class Symbols {
	 public static void main(String[] args) {
	  Scanner read = new Scanner (System.in);
	  int nline, count, countl, nspace= 0, i;
	  char symbol;
	  System.out.print(" how many lines you want to print? ");
	  nline= read.nextInt();
	  System.out.print("what is the symbol you want to use: ");
	  symbol= read.next().charAt(0);
	  for (countl=1; countl<=nline; countl++){
		System.out.println();
		for(count=1; count<=countl; count++)
			System.out.print(symbol);
		 }
	}
}

Output:

 how many lines you want to print? 5
what is the symbol you want to use: +

+
++
+++
++++
+++++

Advertisements

Posted on: December 8, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.