Printing order

Problem statement
  Write a Program to print below figure

   *
  * *
 * * *
* * * *
* * * * *
Accept number of lines 'n' from user.


/* Including the header files */
#include<stdio.h>

/* The execution of the program starts here */
int main() {
int n, i, j, k;

/* a. Read height ( = #lines) of the Pyramid from terminal */
printf("\nEnter n = ");
scanf("%d", &n);
printf("\n");

/* Prints a Pyramid with n asteriks */
if ( n > 0 ) {
for ( i = 0; i < n; i++ ) {
for ( j = 0; j < n - i; j++ ) {
printf(" ");
}
for ( k = 0; k < i+1; k++ ) {
printf(" *");
}
printf("\n");
}
}

printf("\n");
return 0;
}

/*

TEST CASE
------------

$ ./a.out

Enter n = 10

           *
          * *
         * * *
        * * * *
       * * * * *
      * * * * * *
     * * * * * * *
    * * * * * * * *
   * * * * * * * * *
  * * * * * * * * * *

*/

No comments:

Post a Comment