473,383 Members | 1,725 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2. public class Main {
  3.  
  4.     public static void main(String[] args) {
  5.  
  6.       final int N=1;
  7.  
  8.       System.out.println( "Enter the centered number " ); 
  9.       Scanner input =new Scanner(System.in);
  10.       int centeredNumber;
  11.       centeredNumber=input.nextInt();
  12.           for(int x=0;x<centeredNumber;x++)
  13.            {
  14.              String p="";
  15.              for(int k=1 ;k<N+x;k++)
  16.                  p+=k; 
  17.  
  18.              StringBuffer str = new StringBuffer(p);
  19.              System.out.print(str);
  20.              System.out.print(N+x);
  21.              str.reverse();
  22.              System.out.println(str);
  23.  
  24.               if(x==centeredNumber-1)
  25.               {
  26.                   for(int xx=x-1;xx>=0;xx--)
  27.            {
  28.              String pp="";
  29.              for(int k=1  ;k<N+xx;k++)
  30.                  pp+=k; 
  31.  
  32.               StringBuffer str1 = new StringBuffer(pp);
  33.               System.out.print(str1);
  34.               System.out.print(N+xx);
  35.               str1.reverse();
  36.               System.out.println(str1);
  37.  
  38.                   }
  39.               }
  40.  
  41.  
  42.          }
  43.     }
  44.  
  45. }
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
Feb 3 '07 #1
5 2600
Ganon11
3,652 Expert 2GB
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.
Feb 3 '07 #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.

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
Expand|Select|Wrap|Line Numbers
  1.  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
Feb 3 '07 #3
abctech
157 100+
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
Expand|Select|Wrap|Line Numbers
  1. class ABC
  2. {
  3.   public static void main(String args[])
  4.   {
  5.     int i,j,t;
  6.    //ask the user to input the centered number and store it in say 't' 
  7.  
  8.     for (j=1;j<=t;j++) //this 'nested-for' is for creating the upper-triangular-portion
  9.     {            
  10.      for (i=1;i<=t-j;i++)
  11.      {
  12.        System.out.print(" ");
  13.      }
  14.      for (i=1;i<=j;i++)
  15.      {
  16.        System.out.print(i);                
  17.      }
  18.      for (i=j-1;i>=1;i--)
  19.      {
  20.        System.out.print(i);
  21.      }
  22.      System.out.println();
  23.     }
  24.  
  25.     for()//this 'nested-for' is for creating the lower-portion
  26.     {
  27.     --Here--
  28.     }
  29.   }
  30. }
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.
Feb 4 '07 #4
Hi,
I developed this code to solve this problem
I think this is shorter


Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;   //import class Scanner for the input 
  2. public class DiamondNumbers {
  3.     public static void main(String[] args) {
  4.  
  5.       final int N=1;
  6.  
  7.       System.out.println( "Enter your centered number Starting from number 1" ); 
  8.       Scanner input =new Scanner(System.in);
  9.       int centeredNumber;
  10.       centeredNumber=input.nextInt();
  11.           for(int x=0;x<centeredNumber;x++)  //control the count of the lines up to the centered line
  12.             {
  13.                for(int s=1;s<centeredNumber-x;s++)   //print the spaces at the beginning of each line
  14.                    System.out.print(" ");
  15.  
  16.                 String p="";
  17.                 for(int k=1 ;k<N+x;k++)        // print each line of the upper triangular portion
  18.                     p+=k; 
  19.  
  20.                  StringBuffer str = new StringBuffer(p);
  21.                  System.out.print(str);     //print the string of the numbers before the middle number
  22.                  System.out.print(N+x);    //print the middle number of each line
  23.                  str.reverse();          
  24.                  System.out.println(str);   //print the string of the numbers after the middle number
  25.  
  26.  
  27.                 if(x==centeredNumber-1)       //Check if you reached to the centered number or the centered line
  28.                   {
  29.                     for(int xx=x-1;xx>=0;xx--)  //counter for the lines under the centered line 
  30.                       {
  31.  
  32.                         for(int s=1;s<centeredNumber-xx;s++)    //print the spaces at the beginning of each line
  33.                            System.out.print(" ");
  34.  
  35.                          String pp="";
  36.                          for(int k=1  ;k<N+xx;k++)     // print each line of the lower-inverted triangular portion
  37.                              pp+=k; 
  38.  
  39.                           StringBuffer str1 = new StringBuffer(pp);
  40.                           System.out.print(str1);     //print the string of the numbers before the middle number
  41.                           System.out.print(N+xx);     //print the middle number of each line
  42.                           str1.reverse();
  43.                           System.out.println(str1);   //print the string of the numbers after the middle number
  44.  
  45.                         }
  46.                      }
  47.  
  48.                }
  49.   } 
  50. }
Feb 5 '07 #5
abctech
157 100+
Hi,
I developed this code to solve this problem
I think this is shorter


Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;   //import class Scanner for the input 
  2. public class DiamondNumbers {
  3.     public static void main(String[] args) {
  4.  
  5.       final int N=1;
  6.  
  7.       System.out.println( "Enter your centered number Starting from number 1" ); 
  8.       Scanner input =new Scanner(System.in);
  9.       int centeredNumber;
  10.       centeredNumber=input.nextInt();
  11.           for(int x=0;x<centeredNumber;x++)  //control the count of the lines up to the centered line
  12.             {
  13.                for(int s=1;s<centeredNumber-x;s++)   //print the spaces at the beginning of each line
  14.                    System.out.print(" ");
  15.  
  16.                 String p="";
  17.                 for(int k=1 ;k<N+x;k++)        // print each line of the upper triangular portion
  18.                     p+=k; 
  19.  
  20.                  StringBuffer str = new StringBuffer(p);
  21.                  System.out.print(str);     //print the string of the numbers before the middle number
  22.                  System.out.print(N+x);    //print the middle number of each line
  23.                  str.reverse();          
  24.                  System.out.println(str);   //print the string of the numbers after the middle number
  25.  
  26.  
  27.                 if(x==centeredNumber-1)       //Check if you reached to the centered number or the centered line
  28.                   {
  29.                     for(int xx=x-1;xx>=0;xx--)  //counter for the lines under the centered line 
  30.                       {
  31.  
  32.                         for(int s=1;s<centeredNumber-xx;s++)    //print the spaces at the beginning of each line
  33.                            System.out.print(" ");
  34.  
  35.                          String pp="";
  36.                          for(int k=1  ;k<N+xx;k++)     // print each line of the lower-inverted triangular portion
  37.                              pp+=k; 
  38.  
  39.                           StringBuffer str1 = new StringBuffer(pp);
  40.                           System.out.print(str1);     //print the string of the numbers before the middle number
  41.                           System.out.print(N+xx);     //print the middle number of each line
  42.                           str1.reverse();
  43.                           System.out.println(str1);   //print the string of the numbers after the middle number
  44.  
  45.                         }
  46.                      }
  47.  
  48.                }
  49.   } 
  50. }
Whatever works,glad that your program is done!
Cheers
Feb 5 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Phil Powell | last post by:
I am having this problem: My PHP script will set a cookie, it's there in my /Cookies folder. I delete the cookie (I have to for testing purposes, the PHP script I run behaves according to this...
5
by: Bruce | last post by:
I have a number of forms that do significant work based on variables POSTed from the form. What is the common method of detecting and preventing this work from being done when the form is POSTed as...
1
by: Jiten | last post by:
Hi Cor I spoke with u previously about this problem i had and u gave me some code that helped me. That code worked fine but i have now encountered another issuee that im hoping u may know how to...
8
by: jon morgan | last post by:
OK, I'm going to be brave. There is a bug in VS.Net 1.1 that causes random compiler errors. I have raised this issue in posts at least three time in the past couple of months without attracting...
17
by: Gabriel Mejía | last post by:
Services or applications using ActiveX Data Objects (ADO) 2.0 or greater may intermittently return empty recordsets on queries that should be returning valid results. At the time the problem...
3
by: penny336 | last post by:
dear all, i am using vc++ 6.0 sp5 i have a class called Address,it allocated some memory space for streetname and city i first define it as Address add = new Address("Madrian","UK"); ......
11
by: Steven T. Hatton | last post by:
In the past there have been lengthy discussiions regarding the role of header files in C++. People have been very adamat about header files serving as in interface to the implementation. I do...
6
by: teedilo | last post by:
We have an application with a SQL Server 2000 back end that is fairly database intensive -- lots of fairly frequent queries, inserts, updates -- the gamut. The application does not make use of...
34
by: Reinhold Birkenfeld | last post by:
Hi, the arguments in the previous thread were convincing enough, so I made the Path class inherit from str/unicode again. It still can be found in CVS:...
5
by: Mike TI | last post by:
March 24, 2006 Hi all I am new to VB.NET and am using VB.NET 2005. I have an MDI form with a Split Container Control. On demand I am adding and removing User Controls on Panel 2. I am using...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.