# Thread: Print a with a straight vertical line to the left instead of the right.

1. Member
Join Date
Sep 2014
Posts
47
Rep Power
0

## Print a with a straight vertical line to the left instead of the right.

How can I make this program print so the output looks like this ( ignore the - -- lines ):

--------1
-------21
-----321
---4321
-54321

1
21
321
4321
54321

Here is my code below.
Java Code:
```public class c5e18c {
public static void main(String[]args){

int i;
int j;

for(i = 1; i<=6; i++){

for (j = i; j>=1 ;j--){
System.out.printf(j + " ");
}
System.out.println();
}

}```
}

2. ## Re: Print a with a straight vertical line to the left instead of the right.

Yes, you can. What have you tried?

3. Member
Join Date
Oct 2014
Location
Bhagalpur, India
Posts
3
Rep Power
0

## Re: Print a with a straight vertical line to the left instead of the right.

its not that difficult. try to modify the inner loop.

4. Member
Join Date
Sep 2014
Posts
47
Rep Power
0

## Re: Print a with a straight vertical line to the left instead of the right.

Hi guys, thanks for a the replies.

Could a solution could be to with the firstprint 5 spaces, then 4 spaces for the second print and so on..
Not sure how to to do that.
-----1 (5 spaces)
----21(4 spaces)
Is this correct thinking?

Have also tried multiple variations of formatf(), but I have not been able to figure out a way to solve it.
Could I solve this problem by the use of formatf()?

ababa.
How should I modify the innerloop to make it work?

Thanks again for the help!

5. ## Re: Print a with a straight vertical line to the left instead of the right.

Decompose your problem and define smaller problems: define a small method that can print i space characters on a line and define another small method that can print the numbers n, n-1, ... 1 on a line; on line j (j in the interval [1,5]), you want to print 6-j spaces and the numbers j, j-1, ... 1; so:

Java Code:
```for (int j= 1; j <= 5; j++) {
printSpaces(6-j);
printNumbers(j);
System.out.println();
}```
Now you define those two small methods ...

kind regards,

Jos

6. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
13

## Re: Print a with a straight vertical line to the left instead of the right.

You don't need to verify it - you SEE it is correct and you already verified it before you even came here. Each minus represents a space and you can clearly see that it goes from 5 spaces to 4 spaces to 3 spaces etc. etc. By drawing it out you make it visual and undeniable - yet you're still questioning it. Why do you question what you can clearly see?

Try thinking about it again, this time knowing for sure that you've got a correctly working brain. Toss your existing code, it will only distract you. Write out in English what you need to do.

- I need to write a program which will print X lines.
- On line 1 of 6 it will print 5 spaces and the number 1
- On line 2 of 6 it will print 4 spaces and the numbers 2 1
- On line 3 of 6 it will print 3 spaces and the numbers 3 2 1
- On line 4 of 6 it will print 2 spaces and the numbers 4 3 2 1
- On line 5 of 6 it will print 1 space and the numbers 5 4 3 2 1
- On line 6 of 6 it will print 0 spaces and the numbers 6 5 4 3 2 1

You didn't mention the sixth line but the program just seems incomplete to me without it. If you read that plain English, I'm sure you can identify some correlations that will help you to write the code. I cheated a little here by very carefully formulating it such that the truth is in plain sight ;)

7. Member
Join Date
Sep 2014
Posts
47
Rep Power
0

## Re: Print a with a straight vertical line to the left instead of the right.

Originally Posted by JosAH
Decompose your problem and define smaller problems: define a small method that can print i space characters on a line and define another small method that can print the numbers n, n-1, ... 1 on a line; on line j (j in the interval [1,5]), you want to print 6-j spaces and the numbers j, j-1, ... 1; so:

Java Code:
```for (int j= 1; j <= 5; j++) {
printSpaces(6-j);
printNumbers(j);
System.out.println();
}```
Now you define those two small methods ...

kind regards,

Jos
I created an aditional for loop for pritning spaces, the loop prints 6-i number of spaces.
It gave me the output format I wanted. Is this a good way of doing it?

Java Code:
```public class c5e18c {
public static void main(String[]args){

int i;
int j;
int k;

for(i = 1; i<=6; i++){

for( k= 6; k>=i ; k--){
System.out.print("  ");
}

for (j = i; j>=1 ;j--){
System.out.print(" " + j);
}
System.out.println();

}

}
}```
Last edited by Thammer; 10-28-2014 at 11:37 AM.

8. Member
Join Date
Sep 2014
Posts
47
Rep Power
0

## Re: Print a with a straight vertical line to the left instead of the right.

Originally Posted by gimbal2
You don't need to verify it - you SEE it is correct and you already verified it before you even came here. Each minus represents a space and you can clearly see that it goes from 5 spaces to 4 spaces to 3 spaces etc. etc. By drawing it out you make it visual and undeniable - yet you're still questioning it. Why do you question what you can clearly see?

Try thinking about it again, this time knowing for sure that you've got a correctly working brain. Toss your existing code, it will only distract you. Write out in English what you need to do.

- I need to write a program which will print X lines.
- On line 1 of 6 it will print 5 spaces and the number 1
- On line 2 of 6 it will print 4 spaces and the numbers 2 1
- On line 3 of 6 it will print 3 spaces and the numbers 3 2 1
- On line 4 of 6 it will print 2 spaces and the numbers 4 3 2 1
- On line 5 of 6 it will print 1 space and the numbers 5 4 3 2 1
- On line 6 of 6 it will print 0 spaces and the numbers 6 5 4 3 2 1

You didn't mention the sixth line but the program just seems incomplete to me without it. If you read that plain English, I'm sure you can identify some correlations that will help you to write the code. I cheated a little here by very carefully formulating it such that the truth is in plain sight ;)
The problem for me was to code how to get the program to print the amount of spaces I wanted per line, but I got it to work now :)
Thanks!

9. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
13

## Re: Print a with a straight vertical line to the left instead of the right.

Any way that works is a "good" way.

Question is: do you think it is good enough? I see three loops. I wonder if you can do it with only one with those correlations I mentioned earlier. Some things you need to "count" with a loop, some things you can simply derive from information you already have. One piece of information you have is that the length of a line is 6 characters. For each line, you know the line number which you can number from 1 to 6. With those two pieces of information, you know basically everything there is to know. For example, numberOfSpaces = lineLength - lineNumber.

10. Member
Join Date
Sep 2014
Posts
47
Rep Power
0

## Re: Print a with a straight vertical line to the left instead of the right.

Originally Posted by gimbal2
Any way that works is a "good" way.

Question is: do you think it is good enough? I see three loops. I wonder if you can do it with only one with those correlations I mentioned earlier. Some things you need to "count" with a loop, some things you can simply derive from information you already have. One piece of information you have is that the length of a line is 6 characters. For each line, you know the line number which you can number from 1 to 6. With those two pieces of information, you know basically everything there is to know. For example, numberOfSpaces = lineLength - lineNumber.
Interesting point.
I will give it a try and see if I can reduce the amount of loops used :)

11. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Print a with a straight vertical line to the left instead of the right.

You can do also do it using a string with a simple System.out.printf() statement using an appropriate width specifier. Check out the Formatter class for more info.

Regards,
Jim

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•