Home > OS >  Print half left pyramid/triangle and half right pyramid/triangle together
Print half left pyramid/triangle and half right pyramid/triangle together

Time:06-11

Newb Here!

Trying some things with C Language, I tried to print Half left triangle/pyramid and Half right triangle/pyramid together in one print.

heres the code!

#include <stdio.h>  
#include <conio.h>
int main()  
{  
    int i, j, rows, k;  
    printf (" Enter a number to define the rows: \n ");  
scanf("%d", &rows);  
printf("\n");  
for (i = 1; i <= rows;   i) // outer loop  
{  
    for (j = 1; j <= i;   j) // inner loop  
    {  
        printf ("*"); // print the Star  
    }  
    printf ("\n");   
}  
{ 
for (i = 1; i <= rows; i  )   
{  
    for (j = i; j < rows; j  )  
    {  
        printf(" ");   
    }  
    for (k = 1; k <= i; k  )  
    {  
        printf("*"); // print the Star  
    }  
    printf ("\n");   
  }  
 }  
}

CodePudding user response:

#include <stdio.h>  
#include <conio.h>
int main()  
{  
    int i, j, rows, k;
    int hollow_spacing = 2;
    int left_padding = 10;
    printf (" Enter a number to define the rows: \n ");  
    scanf("%d", &rows);
    printf("\n");  
    for (i = 0; i < rows; i  )
    {
        int curr_left_pad = left_padding   rows - i;
        while(curr_left_pad-- > 0){
            printf (" ");
        }

        for (int j = i; j >= 0; j--)
            printf ("*");
        for (int j = 0; j < hollow_spacing;   j)
            printf (" ");
        for (int j = i; j >= 0; j--)
            printf ("*");
        
        printf ("\n");   
    }
}

enter image description here

  •  Tags:  
  • c
  • Related