Thread: tringle pyramid

  1. #1
    Registered User
    Join Date
    Oct 2022
    Posts
    89

    tringle pyramid

    I want to print following pyramid

    Code:
        *   
       **
      ***
     ****
    *****
    My code
    Code:
     #include<stdio.h>
    
    int main ()
    {
        int i, j = 0;
        
        for ( i = 0; i < 5; i++)
        {
            for ( j = 0; j < 5; j++)
            {
                printf("*");
            }    
            printf("\n");
        }
        return 0;
    }
    My logic to print pyramid

    i = 0 j = 4
    i = 1 j = 3, j = 4
    i = 2 j = 2 j = 3, j = 4
    i = 3 j = 1, j = 2, j = 3, j = 4
    i = 4 j = 0, j = 1, j = 2 j = 3, j = 4

    I don't understand how to convert logic into code

    any help please
    Last edited by Kittu20; 12-19-2022 at 02:51 AM.

  2. #2
    Registered User
    Join Date
    Feb 2022
    Location
    Canada, PEI
    Posts
    103
    Quote Originally Posted by Kittu20 View Post
    I want to print following pyramid
    I don't understand how to convert logic into code
    any help please
    i.e. I don't know how to program.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Start with better variable names.

    Don't be afraid to create further ones to add meaning.

    Don't be afraid to create functions to do bits of repetitive work.

    Code:
    void print_symbol(char c, int n) {
      for ( int i = 0 ; i < n ; i++ ) putchar(c);
    }
    
    int main ( ) {
      for ( row = 0 ; row < 5 ; row++ ) {
        num_spaces = row;
        num_stars = row;
        print_symbol(' ', num_spaces);
        print_symbol('*', num_stars);
        printf("\n");
      } 
    }
    Now all you need is the right calculations.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Oct 2022
    Posts
    89
    Quote Originally Posted by Salem View Post
    Start with better variable names.

    Don't be afraid to create further ones to add meaning.

    Don't be afraid to create functions to do bits of repetitive work.

    Code:
    void print_symbol(char c, int n) {
      for ( int i = 0 ; i < n ; i++ ) putchar(c);
    }
    
    int main ( ) {
      for ( row = 0 ; row < 5 ; row++ ) {
        num_spaces = row;
        num_stars = row;
        print_symbol(' ', num_spaces);
        print_symbol('*', num_stars);
        printf("\n");
      }
    }
    Now all you need is the right calculations.
    I tried your approach and I have written code. I get expected output but there is one problem I am getting first empty column and I don't know how to remove it

    Code:
    #include<stdio.h>
    
    void print_symbol(char c, int n) {
      for ( int i = 0 ; i < n ; i++ )
    putchar(c);
    
    
    }
     
    void print_symbol2(char c, int n) {
      for ( int i = 5 ; i >=n ; i-- )
    putchar(c);
    
    
    }
    int main ( ) 
    {
    	
    	int row,  num_spaces, num_stars;
    	
      for ( row = 5  ; row > 0; row-- ) 
      {
        num_spaces = row;
        num_stars = row;
        print_symbol(' ',  num_spaces);
        print_symbol2('*', num_stars);
        printf("\n");
      } 
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Why do you need two print_symbol functions?
    You don't need two.

    > num_spaces = row;
    > num_stars = row;
    These were EXAMPLES!
    Not something your you to copy/paste.

    You need to figure out a proper calculation for each variable.
    - The number of spaces decreases by row
    - The number of stars increases by row
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Aug 2022
    Posts
    40
    Hey Kittu20, That looks like a problem I solved a few months ago from a "C Programming" book by K.N. King. Did you get it resolved?

  7. #7
    Registered User
    Join Date
    Feb 2022
    Location
    Canada, PEI
    Posts
    103
    You should try simplifying the problem.

    First try and write a program that creates this output
    Code:
    *
    *
    *
    *
    *
    Then create a program that indents each asterisk one place to the right.
    Code:
     *
     *
     *
     *
     *
    If you can accomplish the above then you should be able to create the triangle.

  8. #8
    Registered User
    Join Date
    Oct 2022
    Posts
    89
    Quote Originally Posted by Salem View Post
    Why do you need two print_symbol functions?
    You don't need two.

    > num_spaces = row;
    > num_stars = row;
    These were EXAMPLES!
    Not something your you to copy/paste.

    You need to figure out a proper calculation for each variable.
    - The number of spaces decreases by row
    - The number of stars increases by row
    I tried hard to think about the calculation but I can't figure out right calculations. Can you give any clues

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I tried hard to think about the calculation but I can't figure out right calculations. Can you give any clues
    Dunno, I thought you'd already figured it out in post #1

    But maybe
    spaces = 5 - row;
    stars = row + 1;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Aug 2022
    Posts
    40
    Hey Kittu20, where did you get this question? I've run into it before and I can't remember. Try this code:

    Code:
    #include<stdio.h>
    
    
    int main (){
        int i, j;
    
    
        for (i=0;i<=5;i++){
            for (j=5;j>=0;j--){
                if(i<j){
                    printf(" ");
    
    
                }
                else{
                    printf("*");
                }
            }
            printf("\n");
        }
        return 0;
    }

  11. #11
    Registered User
    Join Date
    Nov 2022
    Posts
    8
    Just have to pad the spaces first and then draw a star.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Empty tringle
    By chaklader in forum C Programming
    Replies: 2
    Last Post: 12-11-2010, 07:28 PM
  2. how make this all pyramid in c++ 4.5
    By shah18 in forum C Programming
    Replies: 5
    Last Post: 10-04-2010, 11:34 AM
  3. stupid pyramid
    By nikki19 in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2002, 11:33 PM

Tags for this Thread