473,468 Members | 1,466 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

the problem again

36 New Member
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 2604
Ganon11
3,652 Recognized Expert Specialist
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
analoveu
36 New Member
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 New Member
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
analoveu
36 New Member
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 New Member
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: 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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.