Advertisement
defineSN

printing star pattern in C ver1.2

Nov 17th, 2012
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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,choice=0;
  34.     for(int z=0;z<100;z++){
  35.         printf("---");
  36.     }
  37.     puts("\n\n");
  38.     printf("1.normal or 2.inverted? enter choice: ");
  39.     scanf("%d",&choice);
  40.     printf("enter the number of rows: ");
  41.     scanf("%d",&n);
  42.    
  43.     if(choice==1){
  44.         temp=n;
  45.         for(rows=1;rows<=n;rows++,temp--){//this is for number of rows
  46.                
  47.                 for(int i=0;i<temp;i++){// this is for printing spaces
  48.                         printf(" ");
  49.                 }
  50.                 for(int i=0;i<(2*rows-1);i++){// this is for printing stars
  51.                         printf("*");
  52.                 }
  53.                 printf("\n");
  54.         }  
  55.    
  56.     }
  57.     else if(choice==2){
  58.    
  59.         //temp=n;
  60.         for(rows=n;rows>0;rows--,temp++){//this is for number of rows
  61.            
  62.             for(int i=0;i<temp;i++){// this is for printing spaces
  63.                 printf(" ");
  64.             }
  65.             for(int i=0;i<(2*rows-1);i++){// this is for printing stars
  66.                 printf("*");
  67.             }
  68.             printf("\n");
  69.         }
  70.     }  
  71. }
  72.  
  73. void leftPyramid(){
  74.    
  75.     int rows=1,n=0,temp=0;
  76.     for(int z=0;z<100;z++){
  77.         printf("---");
  78.     }
  79.     puts("\n\n");
  80.    
  81.     printf("enter the number of rows: ");
  82.     scanf("%d",&n);
  83.    
  84.     temp=n;
  85.     for(rows=1;rows<=n;rows++,temp--){//this is for number of rows
  86.        
  87.         for(int i=0;i<temp;i++){// this is for printing spaces
  88.             printf(" ");
  89.         }
  90.         for(int i=0;i<rows;i++){// this is for printing stars
  91.             printf("*");
  92.         }
  93.         printf("\n");
  94.     }
  95. }
  96.  
  97. void rightPyramid(){
  98.     int rows=1,n=0,temp=0;
  99.     for(int z=0;z<100;z++){
  100.         printf("---");
  101.     }
  102.     puts("\n\n");
  103.    
  104.     printf("enter the number of rows: ");
  105.     scanf("%d",&n);
  106.    
  107.     temp=n;
  108.     for(rows=1;rows<=n;rows++,temp--){//this is for number of rows
  109.  
  110.         for(int i=0;i<rows;i++){// this is for printing stars
  111.             printf("*");
  112.         }  
  113.         for(int i=0;i<temp;i++){// this is for printing spaces
  114.             printf(" ");
  115.         }
  116.         printf("\n");
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement