This blog is under construction

Friday 12 July 2013

C program to print right angled triangle

Write a C program to print right angled triangle.


  #include <stdio.h>

  int main() {
        int i, j, k, n, value = 1;

        /* get the number of rows rows from the user */
        printf("Enter your input value(1-20):");
        scanf("%d", &n);

        /* prints right angled traingle */
        for (i = 1; i <= n; i++) {
                /* space infront of triangle */
                printf("     ");
                for (j = 1; j <= (n + 1) - i; j++) {
                        printf("  ");
                }

                /*
                 * right angled triangle composed
                 * of numbers from 1 to 9
                 */
                for (k = 1; k <= i; k++) {
                        if (value > 9)
                                value = 1;
                        printf("%d ", value++);
                }
                printf("\n");
        }

        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter your input value(1-20):10
                          1 
                        2 3 
                      4 5 6 
                    7 8 9 1 
                  2 3 4 5 6 
                7 8 9 1 2 3 
              4 5 6 7 8 9 1 
            2 3 4 5 6 7 8 9 
         1 2 3 4 5 6 7 8 9 
       1 2 3 4 5 6 7 8 9 1 




No comments:

Post a Comment