Click here to Skip to main content
15,921,660 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 ?

Thanks in Advance !! :)
Posted
Updated 22-Sep-16 8:44am
v5

1 solution

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.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
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.
 
Share this answer
 
Comments
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    
There is 2 links in my answer.
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.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



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