Make triangle with '*' & print number (School :()


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: Make triangle with '*' & print number (School :()

  1. #1
    Join Date
    Apr 2006
    Posts
    5

    Unhappy Make triangle with '*' & print number (School :()

    Hello all...
    I am new to these forums and i need some help regarding java for the university... Here are 2 things i have to make as java classes but i dont know how:

    1st) Print triagle : Read a positive number posted by user N (N>=3) and print a triangle with asterisks that will be N lines (see example)
    (eg N=4)
    Code:
        *
      ***
     *****
    *******
    Its something with loop but i dunno what

    2nd) Print number: Read a user submited number from 0 to 99999 and post the ammount of the digits of the number submited. Next print same number (the one submited) in a 5 fields format Eg number 13, digits=2 and print as 00013

    If you help me in both i take a good grade for current lab and i will be very happy for it

    Hope i get replies

  2. #2
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    255
    Hint for the first, run the loop from 1 to N and print 2N -1 asterisks on each line.
    Happiness is good health and a bad memory.

  3. #3
    Join Date
    Apr 2006
    Posts
    5
    hhMMM im new to java (i only used php in the past) so can u be more explainable?
    I just dont know what to print and how it print (println or print) in the loop

  4. #4
    Join Date
    Apr 2006
    Posts
    5
    I found the solution for 2nd code, its the following :
    Code:
    					System.out.print("Input number R:");
    					int R = Integer.parseInt(br.readLine());	
    					while (R<0 || R>99999) {
    						System.out.println("Error. Wrong Input. Try Again");
    						System.out.print("Input number R:");
    						R = Integer.parseInt(br.readLine());
    					System.out.print("The digit number is :");
    					}
    					int countzero = 0; // Apothikeusi Midenikwn Stixeiwn
    					if (R<10) {
    						System.out.println("1 Digit");
    						countzero = 4;
    					} else if (R<100) {
    						System.out.println("2 Digits");
    						countzero = 3;
    					} else if (R<1000) {
    						System.out.println("3 Digits");
    						countzero = 2;
    					} else if (R<10000) {
    						System.out.println("4 Digits");
    						countzero = 1;
    					} else if (R<100000) {
    						System.out.println("5 Digits");
    						countzero = 0;
    					}
    					for (int g=0; g<countzero; g++)
    						System.out.print("0");
    					System.out.println(R);
    I must make the tree only now

  5. #5
    Join Date
    Jul 2005
    Location
    the Netherlands
    Posts
    128
    Code:
    for(int i = 1; i <= n; i++) { // n is the number of lines
    
      int numOfSpaces = /* ? */;
      int numOfAsterisks = /* ? */;
                
      for(int j = 1; j <= numOfSpaces; j++) 
        System.out.print(' ');
                
      for(int j = 1; j <= numOfAsterisks; j++)
        System.out.print('*');
                
      System.out.print('\n');
    }
    Try to finish it now. If you don't see it immediatelly, write down on paper the number of spaces and asterixes for each line. Try to see how those number relate to i.
    Last edited by prometheuzz; 04-19-2006 at 05:22 PM.

  6. #6
    Join Date
    Apr 2006
    Posts
    20
    Ok, here's how I did the code for the triangle

    Code:
    import java.util.*;
    import javax.swing.JOptionPane;
    
    public class pyramid{
    	public static void main (String[]Args){
    		
    		int input = 0;
    		String temp_string = JOptionPane.showInputDialog("Please enter an integer");
    		temp_string = temp_string.trim();
    		input= Integer.parseInt(temp_string);
    		
    		for (int i=1; i<=input; i++)
    		{
    			for (int j=1; j<=i; j++)
    			{
    				System.out.print("*");
    			}
    			System.out.println("");
    		}
    	}
    }
    This just gets you an output like

    *
    **
    ***

    as opoosed to being lined up like you said

  7. #7
    Join Date
    Apr 2006
    Posts
    5
    Thank you a lot, i really appreciated ur help

  8. #8
    Join Date
    Apr 2006
    Posts
    2
    PHP Code:

    import javax
    .swing.JOptionPane;

    public class 
    TriangleWithAsterisk {

        public static 
    void main(String[] args)
        {
            
    int n;
            
            
    Integer.valueOf(JOptionPane.showInputDialog(null"Enter the number of lines --> "));
            
            for (
    int i 0ni++)
            {
                
    int stars * (i);
                
    int space i;
                for (
    int j 0spacej++)
                {
                    
    System.out.print("  ");
                }
                
                for (
    int k 0starsk++)
                {
                    
    System.out.print("* ");
                }
                
    System.out.println();
            }
            
        }
    }


    i got an output like this when i entered 6

                

              * * * 
            * * * * * 
          * * * * * * * 
        * * * * * * * * * 
      * * * * * * * * * * * 

  9. #9
    Join Date
    Apr 2006
    Posts
    5
    Geeeeeeeeee thanx a lot
    Really aprreciated your help!

Similar Threads

  1. Re: MAKE $$$$$$$$$$ FAST THIS REALLY WORKS
    By Mark Campisi in forum .NET
    Replies: 0
    Last Post: 07-14-2002, 05:05 PM
  2. OO4O - Needs help me with LONG RAW problem please?
    By Ofir in forum oracle.general
    Replies: 0
    Last Post: 06-24-2002, 06:28 AM
  3. Database questions.
    By Kofi Brown in forum Database
    Replies: 4
    Last Post: 05-09-2000, 10:37 AM
  4. Replies: 2
    Last Post: 03-24-2000, 02:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center