0

I was doing some hackerrank practice and I couldnt solve it. I am newbie any ideas to solve this problem?

Please write a software program that prints the following pyramid pattern to the screen. Your program should get a number “n” as an input and should print n lines of the pyramid. (For example if your input is 4, then the following output is expected.)

(You can choose any programming language that you want.)

      1
    2 3 2
  3 4 5 4 3
4 5 6 7 6 5 4


#include<stdio.h>

int main()
{
    int i,j,k,l=1;
    for(i=1; i<=5; i++)
    {
        for(j=4; j>=i; j--)
        {
            printf(" ");
        }
        
        for(k=1; k<=l; k++)
        { 
            printf("%d",k);
        }            
            l = l+2;    
        printf("\n");
    }
    return 0;
}
5
  • 1
    please share what you tried and we would help you rectify it Commented Feb 11, 2021 at 7:19
  • @Krishna Chaurasia I added what I have done so far. Could you help me to fix that the expected output above ? Commented Feb 11, 2021 at 7:27
  • 1
    The code is in C language but you have added the python tag. Editing the tag to add C will likely get you more help. Commented Feb 11, 2021 at 7:28
  • @Krishna Chaurasia it could be done any language. I open every of them. Could u help Commented Feb 11, 2021 at 7:30
  • 1
    @burakzbc Hint: have a separate loop for the left and right triangle. Also use some maths to calculate the number that needs to be printed. Commented Feb 11, 2021 at 7:37

1 Answer 1

2

These types of problems can be solved by simple mathematics.

I would advice you to solve this by problem by breaking it into simpler and smaller use cases as follows:

  1. Number of blank spaces on each line.

  2. Number of Elements (on each line) until the centre of pyramid.

  3. Number of Elements (on each line) after the centre of pyramid.

     #include<stdio.h>
    
     int main()
     {
         int N;
         scanf("%d", &N);
    
         int numberOfBlankSpaces = N + 2;
    
         for(int i=1; i<=N ;i++){    
    
             for(int j=0;j<numberOfBlankSpaces;j++){
                 printf(" ");
             }
    
             numberOfBlankSpaces -= 2;
    
             int number_of_elements_until_centre_on_each_line = i;
             int startingElement = i;
    
             //Print increasing order until center
             while(number_of_elements_until_centre_on_each_line--){
                 printf("%d ", startingElement);
                 startingElement++;
             }
    
             // Starting element = Element at center - 1 
             startingElement-=2;
    
             //Print decresing order
             while(startingElement >= i){
                 printf("%d ", startingElement);
                 startingElement--;
             }
    
             printf("\n");
         }
    
         return 0;
     }
    

Input: 4

Output:

      1 
    2 3 2 
  3 4 5 4 3 
4 5 6 7 6 5 4 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.