[Solved] How do I change the output to an equilateral triangle of numbers?


The logic of your for loop must be modifies to get an equilateral triangle. The below code works, The outer most for loop I have used is to print the numbers in a new line every time after completing the inner loops the outer loop prints a new line. The first inner loop isto print the space for an equilateral triangle and the second inner loop is to print the numbers.

#include <stdio.h> 
#include <stdlib.h> 

int main()
{
int x,y, rows; 
printf("This is a program that generates a equilateral triangle of n height. \n");
printf("Enter the height of the equilateral triangle: \n");
scanf ("%d",&rows); 

while (rows <= 1 )
{
    printf("Invalid output, it takes two or more rows to produce a triangle.\n");
    printf("Enter the number of rows for the increasing triangle: \n");
    scanf("%d", &rows); 
}

printf("The triangle is as follows: \n"); 
for(x=1; x<=rows; ++x)
    {
        
        for(y=x; y<=rows; ++y)
        {
            printf(" ");
        }
        
        for(y =1; y<=((2*x)-1); ++y)
        {
            printf("%d",x);
        }
        // Print new line
        printf("\n");
    }
return 0;
}

enter image description here

2

solved How do I change the output to an equilateral triangle of numbers?