15,904,024 members
See more:
I am trying to generate this kind of pattern which is based on user input .Here input is num=7

Output

```1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

1 2 3 4 5 6

1 2 3 4 5 6 7

1 2 3 4 5 6

1 2 3 4 5

1 2 3 4

1 2 3

1 2

1```

Here's my code:

C#
```import java.util.Scanner;
class Test
{
public static void main(String arr[])
{
Scanner input=new Scanner(System.in);
System.out.println("Enter a no to print a symmetrical pyramid :");
int num=input.nextInt();

//Printing normal Pyramid

for(int i=0;i<=num;i++)
{
for(int j=1;j<i;j++)
{
System.out.print(j);
}

System.out.println("");
}
//Middle
for(int i=1;i<=num;i++)
{
System.out.print(i);
}
System.out.println("");

//Printing inverted Pyramid

for(int i=num;i>=0;i--)
{
for(int j=1;j<i;j++)
{
System.out.print(j);
}

System.out.println("");

}

}
}```

What I have tried:

How can I achieve the same output using only 1 for loop ?

Posted
Updated 22-Sep-16 8:44am
v5

## Solution 1

We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
So, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.

You have 5 loops in your code.
- You can remove the loop of middle line simply by saying that there is 1 more line in upper triangle.
- You can merge upper and lower triangles with a clever usage of the absolute function (abs()).
- And by using a recursive function, you can simulate a loop without loop.
Real work is left to you since it is HomeWork.

-----------------------
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

Advice: tale a sheet of paper and try to do it by hand, your program should use the same procedure.

Member 12731696 22-Sep-16 15:02pm
Thanks for the advice !! I would definitely follow what you mentioned here. I did this homework but i found that my code is too long but it definitely gives me the required output. I was just wondering whether it can be solved by a different logic .
Member 12731696 22-Sep-16 15:05pm
Also, you mentioned that i can debug my code by my own instead of guessing . I would love to do that. Is there any resource that explains how to Trace the program execution step by step so that by practicing I can effectively debug my own code instead of checking it on the compiler and guessing the output?
Patrice T 22-Sep-16 15:10pm
Look in your IDE, Visual Studio ... ?
Member 12731696 22-Sep-16 15:17pm
I am not using IDE currently, I am working on JDK using a text editor. The paper based debugging can be a help at this stage. Later on i will take an IDE , probably Android Studio.

Top Experts
Last 24hrsThis month
 Pete O'Hanlon 125 Dave Kreskowiak 75 CPallini 50 OriginalGriff 40 Richard Deeming 40
 Pete O'Hanlon 1,705 OriginalGriff 1,267 Richard Deeming 485 Dave Kreskowiak 415 Richard MacCutchan 355

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900