Saturday, December 5, 2015

Pyramid Programs in C - Type #2

Program #6

Description:
Pyramid Type #2

#include "stdio.h"
#include "conio.h"
int main() {
 int i,j,k,n;

 /* clear the screen */
 clrscr();

 printf(" Enter a number to make Type #2 Pramid:\n ");
 scanf("%d", &n);

 /* Validate input */
 if (n < 2 || n > 10){
   return 0;
 }

 /* Make some space between input and Pyramid */
 printf("\n\n");

 for (i=1; i<=n; i++) {
   /* move cursor to center to make pyramid view */
   for (k=1; k<=(n-i); k++) printf(" ");

   for (j=1; j<=i; j++){
     /* display only first and last number in every row */
     if (j==1 || j==i){
 printf("%d ", i);

     } else if (i==n){
 /* print all remaining number in last row to complete Pyramid */
 printf("%d ", i);
     } else if (j<i){

 /* print blank space to avoid pyramid shape */
 printf("  ");
     }

   }
   /* Move to next line */
   printf("\n");
 }

 /* Wait after print Pramid */
 getch();
 return 1;
}



 

No comments:

Post a Comment

Popular Posts