the problem again

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • analoveu
    New Member
    • Jan 2007
    • 36

    the problem again

    I have a problem with printing the spaces at the begining of each line in the following shape

    1
    121
    12321
    1234321
    12321
    121
    1

    but the code i developed print it like that
    1
    121
    12321
    1234321
    12321
    121
    1

    and this is the code i developed

    Code:
    import java.util.Scanner;
    public class Main {
     
        public static void main(String[] args) {
          
          final int N=1;
          
          System.out.println( "Enter the centered number " ); 
          Scanner input =new Scanner(System.in);
          int centeredNumber;
          centeredNumber=input.nextInt();
              for(int x=0;x<centeredNumber;x++)
               {
                 String p="";
                 for(int k=1 ;k<N+x;k++)
                     p+=k; 
                     
                 StringBuffer str = new StringBuffer(p);
                 System.out.print(str);
                 System.out.print(N+x);
                 str.reverse();
                 System.out.println(str);
                  
                  if(x==centeredNumber-1)
                  {
                      for(int xx=x-1;xx>=0;xx--)
               {
                 String pp="";
                 for(int k=1  ;k<N+xx;k++)
                     pp+=k; 
                  
                  StringBuffer str1 = new StringBuffer(pp);
                  System.out.print(str1);
                  System.out.print(N+xx);
                  str1.reverse();
                  System.out.println(str1);
    
                      }
                  }
                 
             
             }
        }
        
    }
    I wanna know how i print the spaces at the begining of each line
    here you will enter the wanted input number ,variable centeredNumber, as 4
    to print the shape i put it
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Suppose the user enters 1. Then you will need to print 0 spaces.

    Suppose the user enters 2. Then you will need to print 1 space on the first line, 0 on the 2nd line, and 1 on the last line.

    Suppose the user enters 3. Then you will need to print 2 spaces on the first line, 1 space on the second line, 0 spaces on the third line, 1 space on the fourth line, and 2 spaces on the fifth line.

    Do you see the pattern emerging?

    Can you generalize this? In other words, fill in the blanks:

    Suppose the user enters ____. Then you will need to print ____ spaces on the first line, ____ spaces on the second line....0 spaces on the middle line...____ spaces on the last line.

    Comment

    • analoveu
      New Member
      • Jan 2007
      • 36

      #3
      Originally posted by Ganon11
      Suppose the user enters 1. Then you will need to print 0 spaces.

      Suppose the user enters 2. Then you will need to print 1 space on the first line, 0 on the 2nd line, and 1 on the last line.

      Suppose the user enters 3. Then you will need to print 2 spaces on the first line, 1 space on the second line, 0 spaces on the third line, 1 space on the fourth line, and 2 spaces on the fifth line.

      Do you see the pattern emerging?

      Can you generalize this? In other words, fill in the blanks:

      Suppose the user enters ____. Then you will need to print ____ spaces on the first line, ____ spaces on the second line....0 spaces on the middle line...____ spaces on the last line.

      ok i know that but my problem is how write code to do that
      how to print 3 spaces using the code
      how to tell it print 2 or 3 spaces , printing spacing like this" " ," " not suitable with my code
      is not there another way to do this?iside the firs for
      Code:
       for(int x=0;x<centeredNumber;x++)
      in the first loop, i want print spaces equal to centeredNumber-1
      in the second loop, i wanna print spaces equal to centeredNumber-2
      ....and so on

      Comment

      • abctech
        New Member
        • Dec 2006
        • 157

        #4
        Hi,
        I used the foll logic for the same program -

        For the upper triangular-part of this diamond shape I'd written the below code:-
        Note:- 't' is the number of lines or say the centered number
        Code:
        class ABC
        {
          public static void main(String args[])
          {
            int i,j,t;
           //ask the user to input the centered number and store it in say 't' 
        
            for (j=1;j<=t;j++) //this 'nested-for' is for creating the upper-triangular-portion
            {			
             for (i=1;i<=t-j;i++)
             {
               System.out.print(" ");
             }
             for (i=1;i<=j;i++)
             {
               System.out.print(i);				
             }
             for (i=j-1;i>=1;i--)
             {
               System.out.print(i);
             }
             System.out.println();
            }
          
            for()//this 'nested-for' is for creating the lower-portion
            {
        	--Here--
            }
          }
        }
        so if the user inputs 4 means t=4 then the above code will generate the o/p as below-
        1
        121
        12321
        1234321
        Now using a similar logic try to develop the second nested-for(--Here--) to print the lower-inverted triangular portion,i.e
        12321
        121
        1
        It will be quite easy if you can understand the first nested-for.
        Last edited by abctech; Feb 4 '07, 03:40 PM. Reason: typo

        Comment

        • analoveu
          New Member
          • Jan 2007
          • 36

          #5
          Hi,
          I developed this code to solve this problem
          I think this is shorter


          Code:
          import java.util.Scanner;   //import class Scanner for the input 
          public class DiamondNumbers {
              public static void main(String[] args) {
          
                final int N=1;
                
                System.out.println( "Enter your centered number Starting from number 1" ); 
                Scanner input =new Scanner(System.in);
                int centeredNumber;
                centeredNumber=input.nextInt();
                    for(int x=0;x<centeredNumber;x++)  //control the count of the lines up to the centered line
                      {
                         for(int s=1;s<centeredNumber-x;s++)   //print the spaces at the beginning of each line
                             System.out.print(" ");
                    
                          String p="";
                          for(int k=1 ;k<N+x;k++)        // print each line of the upper triangular portion
                              p+=k; 
          
                           StringBuffer str = new StringBuffer(p);
                           System.out.print(str);     //print the string of the numbers before the middle number
                           System.out.print(N+x);    //print the middle number of each line
                           str.reverse();          
                           System.out.println(str);   //print the string of the numbers after the middle number
             
                      
                          if(x==centeredNumber-1)       //Check if you reached to the centered number or the centered line
                            {
                              for(int xx=x-1;xx>=0;xx--)  //counter for the lines under the centered line 
                                {
                                 
                                  for(int s=1;s<centeredNumber-xx;s++)    //print the spaces at the beginning of each line
                                     System.out.print(" ");
                               
                                   String pp="";
                                   for(int k=1  ;k<N+xx;k++)     // print each line of the lower-inverted triangular portion
                                       pp+=k; 
          
                                    StringBuffer str1 = new StringBuffer(pp);
                                    System.out.print(str1);     //print the string of the numbers before the middle number
                                    System.out.print(N+xx);     //print the middle number of each line
                                    str1.reverse();
                                    System.out.println(str1);   //print the string of the numbers after the middle number
          
                                  }
                               }
                       
                         }
            } 
          }

          Comment

          • abctech
            New Member
            • Dec 2006
            • 157

            #6
            Originally posted by analoveu
            Hi,
            I developed this code to solve this problem
            I think this is shorter


            Code:
            import java.util.Scanner;   //import class Scanner for the input 
            public class DiamondNumbers {
                public static void main(String[] args) {
            
                  final int N=1;
                  
                  System.out.println( "Enter your centered number Starting from number 1" ); 
                  Scanner input =new Scanner(System.in);
                  int centeredNumber;
                  centeredNumber=input.nextInt();
                      for(int x=0;x<centeredNumber;x++)  //control the count of the lines up to the centered line
                        {
                           for(int s=1;s<centeredNumber-x;s++)   //print the spaces at the beginning of each line
                               System.out.print(" ");
                      
                            String p="";
                            for(int k=1 ;k<N+x;k++)        // print each line of the upper triangular portion
                                p+=k; 
            
                             StringBuffer str = new StringBuffer(p);
                             System.out.print(str);     //print the string of the numbers before the middle number
                             System.out.print(N+x);    //print the middle number of each line
                             str.reverse();          
                             System.out.println(str);   //print the string of the numbers after the middle number
               
                        
                            if(x==centeredNumber-1)       //Check if you reached to the centered number or the centered line
                              {
                                for(int xx=x-1;xx>=0;xx--)  //counter for the lines under the centered line 
                                  {
                                   
                                    for(int s=1;s<centeredNumber-xx;s++)    //print the spaces at the beginning of each line
                                       System.out.print(" ");
                                 
                                     String pp="";
                                     for(int k=1  ;k<N+xx;k++)     // print each line of the lower-inverted triangular portion
                                         pp+=k; 
            
                                      StringBuffer str1 = new StringBuffer(pp);
                                      System.out.print(str1);     //print the string of the numbers before the middle number
                                      System.out.print(N+xx);     //print the middle number of each line
                                      str1.reverse();
                                      System.out.println(str1);   //print the string of the numbers after the middle number
            
                                    }
                                 }
                         
                           }
              } 
            }
            Whatever works,glad that your program is done!
            Cheers

            Comment

            Working...