This blog is under construction

Friday 12 July 2013

C program to make a triangular of stars

Write a C program to make a triangular of stars.


  #include <stdio.h>

  int main() {
        int val, i, j, k;

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

        /* boundary check */
        if (val < 1 || val > 10) {
                printf("Thresh Exceeded!!\n");
        }

        /* construct triangle */
        for (i = 1; i <= val; i++) {
                printf("     ");
                /* space infront of * */
                for (j = i; j < val; j++) {
                        printf("  ");
                }

                /* builds first vertical half of triangle */
                for (k = 1; k <= i; k++) {
                        printf("* ");
                }

                /* builds next half of triangle */
                for (k = 1; k < i; k++) {
                        printf("* ");
                }

                /* moves cursor to new line */
                printf("\n");
        }
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter your input value(1-10):6
               * 
             * * * 
           * * * * * 
         * * * * * * * 
       * * * * * * * * * 
     * * * * * * * * * * * 



No comments:

Post a Comment