13 July 2014

C programs to create pyramid structures

Q1.  Write a C program to create pyramid structure as shown below :

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5


Code:
 //Author : Lakshmikant A. Deshpande  
 #include<stdio.h>  
 #include<stdlib.h>  
 void main()  
 {  
      int i=1,j=5;  
      for(i=1;i<=5;i++)  
      {  
           for(j=1;j<=i;j++)  
                printf("%d ",j);  
      printf("\n");  
      }  
 }  


Q2.  Write a C program to create pyramid structure as shown below :

    1  
   1 2  
  1 2 3
 1 2 3 4
1 2 3 4 5


Code:
 //Author : Lakshmikant A. Deshpande  
 #include<stdio.h>  
 #include<stdlib.h>  
 void main()  
 {  
      int spaces=4;  
      int i,j,k;  
      for(i=1;i<=5;i++)  
      {  
           for(k=0;k<spaces;k++)  //Print spaces
                printf(" ");  
           for(j=1;j<=i;j++)  
           {  
                printf("%d ",j);  
           }  
           for(k=0;k<spaces;k++)  //Print spaces
                printf(" ");  
           printf("\n");  
           spaces--;  
      }  
 }  

2 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete