Home > Back-end >  How do I change the output to an equilateral triangle of numbers?
How do I change the output to an equilateral triangle of numbers?

Time:12-18

I managed to code something that gives me the desired output in a right triangular form. I'm kind of lost on what I must modify to change it into a equilateral triangle

Here's the code:

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

int main()
{
int i, j, 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  (i = 1; i <= rows; i  ) 
{
    for (j =1; j <= rows; j  )  {
     printf("  ");
    }
    for (j = 1; j <= i;j  ) {
        printf("%d", i);
    }
    printf("\n");
}
return 0;
}

CodePudding user response:

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

CodePudding user response:

A very little change will make your code perfect '
1.make your second for loop as for(j=rows-i;j>0;j--)
2.Add a space after the element printf("%d ",i);

or refer this code: ` #include <stdio.h> #include <stdlib.h>

int main()
{
    int i, j, 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  (i = 1; i <= rows; i  ) 
      {
            for (j =rows-i;j>0; j--)
            {
                  printf(" ");
            }
           for (j = 1; j <= i;j  )
           {
                 printf("%d ",i);
           }
                 printf("\n");
       }
           return 0;
      }`
  •  Tags:  
  • c
  • Related