Advertisement
defineSN

printing star pattern in C

Nov 17th, 2012
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.86 KB | None | 0 0
  1. /*this code is to print a pyramid of stars*/
  2.  
  3. #include<stdio.h>
  4.  
  5. void fullPyramid();
  6. void leftPyramid();
  7. void rightPyramid();
  8.  
  9. int main(){
  10.     int choice;
  11.     printf("\nwhat type of star pyramid do u want? \n 1. Full pyramid \n 2. Half pyramid\n enter choice:");
  12.     scanf("%d",&choice);
  13.     puts("\n\n");
  14.     if(choice==1){
  15.         fullPyramid();
  16.     }
  17.     else if(choice==2){
  18.         printf("\n  3.Left Half pyramid\n   4.Right Half pyramid\n  enter choice:");
  19.         scanf("%d",&choice);
  20.        
  21.         if(choice==3){
  22.             leftPyramid();
  23.         }
  24.         else if(choice==4){
  25.             rightPyramid();
  26.         }  
  27.     }
  28.     return 0;
  29. }
  30.  
  31. void fullPyramid(){
  32.    
  33.     int rows=1,n=0,temp=0;
  34.     for(int z=0;z<100;z++){
  35.         printf("---");
  36.     }
  37.     puts("\n\n");
  38.     printf("enter the number of rows: ");
  39.     scanf("%d",&n);
  40.    
  41.     temp=n;
  42.     for(rows=1;rows<=n;rows++,temp--){//this is for number of rows
  43.        
  44.         for(int i=0;i<temp;i++){// this is for printing spaces
  45.             printf(" ");
  46.         }
  47.         for(int i=0;i<(2*rows-1);i++){// this is for printing stars
  48.             printf("*");
  49.         }
  50.         printf("\n");
  51.     }
  52. }
  53.  
  54. void leftPyramid(){
  55.    
  56.     int rows=1,n=0,temp=0;
  57.     for(int z=0;z<100;z++){
  58.         printf("---");
  59.     }
  60.     puts("\n\n");
  61.    
  62.     printf("enter the number of rows: ");
  63.     scanf("%d",&n);
  64.    
  65.     temp=n;
  66.     for(rows=1;rows<=n;rows++,temp--){//this is for number of rows
  67.        
  68.         for(int i=0;i<temp;i++){// this is for printing spaces
  69.             printf(" ");
  70.         }
  71.         for(int i=0;i<rows;i++){// this is for printing stars
  72.             printf("*");
  73.         }
  74.         printf("\n");
  75.     }
  76. }
  77.  
  78. void rightPyramid(){
  79.     int rows=1,n=0,temp=0;
  80.     for(int z=0;z<100;z++){
  81.         printf("---");
  82.     }
  83.     puts("\n\n");
  84.    
  85.     printf("enter the number of rows: ");
  86.     scanf("%d",&n);
  87.    
  88.     temp=n;
  89.     for(rows=1;rows<=n;rows++,temp--){//this is for number of rows
  90.  
  91.         for(int i=0;i<rows;i++){// this is for printing stars
  92.             printf("*");
  93.         }  
  94.         for(int i=0;i<temp;i++){// this is for printing spaces
  95.             printf(" ");
  96.         }
  97.         printf("\n");
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement