Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Triangle Printing - Java Program

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Talking Triangle Printing - Java Program

    Hey all

    With this code you can print a Triangle

    Reg

    public class TrianglePrinting {
     
    	/*
    	 * Prints the numbers in the right angle manner.
    	 */
    	public static void TrianglePrinting(int n) {
    		int counter = 0, i = 1;
    		while (i <= n) {
    			counter++;
    			for (int j = 0; j < counter; j++) {
    				if (i > n)
    					break;
    				System.out.print(i + " ");
    				i++;
    			}
    			System.out.println();
    		}
    	}
     
    	public static void main(String[] args) {
    		TrianglePrinting(14);
    	}
     
    }

    Output:
    1
    2 3
    4 5 6
    7 8 9 10
    11 12 13 14


  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    16
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Triangle Printing - Java Program

    I got other version of that program that asks the user the high of the triangle, and the symbol that he/she wants to use to print the triangle, I also wrote a simple menu
    just to show;

    public class Triangle {
     
    	public static void main(String[] args) {
     
    		Scanner input = new Scanner (System.in);
    		int optiona =0;
     
    		do{	
    			optiona = optiona();
     
    			switch (opcao){
    			case 0:	
                                   System.exit()
                            break;
     
                            case 1:
    			       System.out.println("Insert the tringle's high :");
    			       int high = input.nextInt();	
     
    			       System.out.println();
    			       System.out.println("Insert the triangle's symbol :");
    			       char symbol = input.next().charAt(0);
     
    			       for (int l=1; l <= altura; l++ ){
     
    				       printSequence(high-l,' ');	
    				       printSequence(2*l-1,symbol);
    				       System.out.println();
    		               }
                            break;
    		}while(opcaoa != 0);
    	}
     
    	/**
    	 * Prints in the screen a sequence with n copys of c 
    	 * @param n size of the sequence
    	 * @param c character of the sequence
    	 */
    	public static void printSequence(int n, char c) {
    		for (int i=1; i <= n; i++ )
    			System.out.print(c);
    	}
     
    	public static int optiona(){ //Prints the menu until the option is valid
     
    		int optiona =0;
    		do{ 
    			Scanner input = new Scanner (System.in);
    			System.out.println("Choose an option:");
    			System.out.println("		0 - Exit");
    			System.out.println("		1 - Create triangle");
    			opcaoa = input.nextInt();
     
    			if (optiona <0 || optiona >1) // verify if the option is valid
    				System.out.println("Invalid option!, please choose another option");
    		}while(optiona <0 || optiona>1);
    	return optiona;
    	}
    }


    Sorry if something wrong, and hope some comments (new in forum, and still getting used to it)

Similar Threads

  1. Equilateral Triangle Using nested for loop
    By uchizenmaru in forum Loops & Control Statements
    Replies: 2
    Last Post: January 14th, 2010, 10:01 PM
  2. Triangle Question
    By Leeds_Champion in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 29th, 2009, 11:33 PM
  3. Printing a JTable
    By hundu in forum AWT / Java Swing
    Replies: 0
    Last Post: June 29th, 2009, 08:15 AM
  4. How to printing a Jtable
    By hundu in forum AWT / Java Swing
    Replies: 0
    Last Post: June 29th, 2009, 06:57 AM
  5. Printing JTable that retrieve data from the Database
    By hundu in forum AWT / Java Swing
    Replies: 3
    Last Post: June 28th, 2009, 01:50 PM