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 2600
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: 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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
| |