This blog is under construction

Saturday 22 June 2013

C program to generate pyramid of numbers - II

Write a C program to print pyramid of given numbers.

               1   
            2     2   
         3     3     3   
      4     4     4     4  



  #include <stdio.h>
  int main() {
        int i, j, k, n;
        printf("Enter the value for n:");
        scanf("%d", &n);
        for (i = 1; i <= n; i++) {

                /* loop for space at the front of triangle */
                for (j = i; j <= n; j++) {
                        printf("   ");
                }

                /* prints value and space b/w values */
                for (k = 1; k <= i; k++) {
                        printf("%3d   ", i);
                }

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



  Output:
  jp@jp-VirtualBox:~/cpgms/lab_pgms/02_control_flow$ ./a.out
  Enter the value for n:4
              1   
           2     2   
        3     3     3   
     4     4     4     4   



No comments:

Post a Comment