1. Member
Join Date
Apr 2014
Posts
4
Rep Power
0

## help me out

1. Write a program to produce a similar output for n characters, where n is a odd number. The below output is for n=7.
Java Code:
```A B C D E F G F E D C B A
A B C D E F     F E D C B A
A B C D E           E D C B A
A B C D                 D C B A
A B C                       C B A
A B                             B A
A                                   A```
Last edited by pranesh; 04-26-2014 at 02:36 PM. Reason: added [code] ... [/code] tags

2. ## Re: help me out

I have never seen Donald Duck drawn that badly ... please fix it so we can see what the output is supposed to be.

kind regards,

Jos

3. Senior Member
Join Date
Feb 2014
Posts
447
Rep Power
5

## Re: help me out

Hi,

I understood that you want this output:

printing n characters and then the first n-1 in backwards
printing n-1 characters and then 1*2 blanks and then n-2 characters.
...
printing n-c characters then 1*n blanks and then n-(c+1) characters backwards
and so on till c is n-1

So is this the algorithm? Maybe the way how I wrote it down can help you to automate it with a loop.
Maybe try printing numbers first instead of characters ...

With kind regards,

4. Member
Join Date
Apr 2014
Posts
6
Rep Power
0

## Re: help me out

It seems like he is trying to print out the shape of a pyramid. Nested for loops, using a range of characters. Such that the output looks like

Java Code:
```a  b  c  d  e  f  g
a  b  c      e  f  g
a  b             f  g
a                   g```
So it's the outline of a pyramid.

5. Member
Join Date
Apr 2014
Posts
6
Rep Power
0

## Re: help me out

Yeah, that's impossible to get right.

6. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,155
Rep Power
12

## Re: help me out

Assuming you aren't using recursion, it only takes a single loop. Check out the StringBuilder class. There are some methods there that could be useful.

Regards,
Jim

7. Member
Join Date
Apr 2014
Posts
4
Rep Power
0

## Re: help me out

Originally Posted by kneitzel
Hi,

I understood that you want this output:

printing n characters and then the first n-1 in backwards
printing n-1 characters and then 1*2 blanks and then n-2 characters.
...
printing n-c characters then 1*n blanks and then n-(c+1) characters backwards
and so on till c is n-1

So is this the algorithm? Maybe the way how I wrote it down can help you to automate it with a loop.
Maybe try printing numbers first instead of characters ...

With kind regards,

Sir i could able to understand the concept but i couldnt able to write down the code properly that is the reason i asked for help. Can u help me with the code

8. ## Re: help me out

If you have the concept that might be a starting point for working on the code.
Can you list the steps the program must take to write out the characters on the first line?
And the same for the second line
etc to the last line.

Now look at the steps for each line and see what the pattern is. What changes from line to line.

9. Member
Join Date
Apr 2014
Posts
4
Rep Power
0

## Re: help me out

import java.io.*;

class Alpha{
public static void main(String args[])throws IOException
{
DataInputStream in=new DataInputStream(System.in);
int n;
System.out.println("enter the pattern value");
int a=n+64;
for(int h=0;h<n;h++)
{
for(int i=65;i<=a;i++){
System.out.printf("%c",i);
System.out.printf("\t");
}
System.out.print("");
for(int j=a;j>=65;j--)
{
System.out.printf("%c",j);
System.out.printf("\t");
}
System.out.println("");
a--;
}
}}

can help resolve it for me

10. ## Re: help me out

[code]
[/code]
to get highlighting and preserve formatting.

Please post the program's output so we can see what it does.
I don't see any comments so I don't know what the sections of code are trying to do.

The code would be more understandable if 'A' were used in place of the magic number 65

11. Member
Join Date
Apr 2014
Posts
4
Rep Power
0

## Re: help me out

Java Code:
```import java.io.*;
class Alpha{
public static void main(String args[])throws IOException
{
DataInputStream in=new DataInputStream(System.in);
int n;
System.out.println("enter the pattern value");
int a=n+64;
for(int h=0;h<n;h++)
{
for(int i=65;i<=a;i++){
System.out.printf("%c",i);
System.out.printf("\t");
}
System.out.print("");
for(int j=a;j>=65;j--)
{
System.out.printf("%c",j);
System.out.printf("\t");
}
System.out.println("");
a--;
}
}}```
Required output
Java Code:
```A B C D E F G F E D C B A
A B C D E F     F E D C B A
A B C D E            E D C B A
A B C D                   D C B A
A B C                          C B A
A B                                 B A
A                                        A```
Last edited by Norm; 04-26-2014 at 05:05 PM. Reason: added code tags for output

12. ## Re: help me out

The code has lost all its formatting. The nested statements should be indented to show the logic.

Where are the comments describing what each section of code is to do?
What is the variable: a used for? The name of a variable should describe what the variable holds.

You've posted the Required output.
What is the current output from the program?

The output needs to be wrapped with code tags to preserve the embedded spaces.
Last edited by Norm; 04-26-2014 at 02:47 PM.

13. Senior Member
Join Date
Feb 2014
Posts
447
Rep Power
5

## Re: help me out

Hi,

the output seems to be the current output. At least that is what I go when I was running the code.

So one of the missing points are the spaces in the middle. You used tab instead of spaces which is quite good.
So what you have right now is the outer loop which prints the lines. Inside you have 2 loops that are writing the characters.
Between these 2 loops you have to write some tabs. The first run through the loop (h is 0) you have to write no tabs. Then 2 characters are gone so next time (h=1) you have to write 2 tabs. Then 4 characters are gone and you have to write 4 tabs.
So do you see a possible logic to get the number of tabs printed?

With kind regards,

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

## Re: help me out

Why are you printing out empty strings? (e.g. "").

Regards,
Jim

15. ## Re: help me out

One is to get the newline. It'd be nice if there were comments to see why the code does what it does.

#### Posting Permissions

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