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 - 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
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.
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 - 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
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 - 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.
Hi,
I developed this code to solve this problem
I think this is shorter - 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
-
-
}
-
}
-
-
}
-
}
-
}
Hi,
I developed this code to solve this problem
I think this is shorter - 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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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");
......
|
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...
|
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...
|
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:...
|
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...
|
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: 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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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 ...
| |