Pages

Saturday, 23 April 2016

Star Pattern Program

1.Print a pattern * * * * * ** * * *

To print this pattern we have to use three for loop.Inside the first loop there  will be two loop, one is for controlling the spacing factor and another for  printing the stars.Constant value of First loop is used in second inner loop to decided the number of stars.Here is the source code copy and paste it on code::blocks and run the program youwill see the result.
Source code

#include<stdio.h>
int main() {     char star = '*';     int i, j, num = 4, spc;     for (i = 1; i <= 5; i++) {          for (spc = num; spc >= 1; spc--) { // Spacing factor
               printf(" ");
          }
          for (j = 1; j <= i; j++) {               printf("%2c", star);
     
          printf("\n");
          --num;   // Controls the spacing factor
     }
     return 0;
     }
}
2.Print a pattern* ** * * * * ** * * * * * * * * ** * * * * * * * * * *
This pattern is also simple just joining two triangle in a little different way.Here odd number of star is printed from first and last position.#include<stdio.h>
int main(){
     //char star = '*';
     int i,j;
     for (i=1;i<=7;i++){
          for(j=0;j<11;j++){
               if(i%2!=0){
                    if(j<= i-1||j>=11-i)
                         printf("* ");
                    else
                         printf("  ");
               }

          }
          printf("\n");
     }
}


3.Print a pattern
                     *                  * * *                * * * * *              * * * * * * *                * * * * *                  * * *                    *
                    
/*DISPLAYING THE DIAMOND * *** ***** ******* ***** *** * */
#include<stdio.h>int main(){     int i,j,l,n,k=0;     printf("Enter the rows");     scanf("%d",&n);     for(i=1;i<=n;i++){          for(l=n;l>i;l--){               printf(" ");          }          for(j=1;j<=i+k;j++){               printf("*");
          }
          k++;          printf("\n");     }     int k1=n-2;     for(i=n-1;i>=1;i--){          for(l=i;l<=n-1;l++){               printf(" ");          }          for(j=i+k1;j>=1;j--){               printf("*");          }          k1--;          printf("\n");     }
     return 0;}

4.Print a pattern fish
          *      *        * *    **      * * *  ***     * * * *****      * * *  ***        * *    **         *      * 

#include<cstdio>int main(){     int i,j,k,h,m,n;     printf("Please enter the rows of fish\n");     scanf("%d",&n);     //PRINTING THE UPWARD PART     for(i=n;i>=1;i--){          for(j=i;j>1;j--){               printf(" ");          }          for(k=n;k>=i;k--){               if(k==i) printf("*");               else printf("* ");          }          for(h=i;h>1;h--){               printf(" ");           }          for(m=n;m>=i;m--){               printf("*");          }          printf("\n");     }     //PRINTING THE DOWNARD PART     for(i=1;i<=n-1;i++){          for(j=1;j<i;j++){               printf(" ");          }          for(k=n-1;k>=i;k--){               printf(" *");          }          for(h=1;h<=i;h++){               printf(" ");          }          for(m=i;m<=n-1;m++){               printf("*");          }          printf("\n");     }
     return 0; }

5.Print a pattern
    *    **    ***    ****    *****    ****    ***    **    * 


#include<stdio.h>
int main(){     int i,j,n;     printf("Please enter the rows for the right andled triangle \n");     scanf("%d",&n);     for(i=1;i<=n;i++){          for(j=1;j<=i;j++){               printf("*");          }          printf("\n");
     }     for(i=n-1;i>=1;i--){          for(j=1;j<=i;j++){               printf("*");          }          printf("\n");     }     return 0; }  
6.Print a pattern
             *            **          ***         ****        *****         ****          ***            **             * 
#include<stdio.h>
int main(){     int i,j,k,n;     printf("Please enter the rows for the right andled triangle \n");     scanf("%d",&n);     for(i=n;i>=1;i--){          for(k=1;k<i;k++){
               printf(" ");
          }          for(j=n;j>=i;j--){               printf("*");
          }          printf("\n");     }     for(i=1;i<=n-1;i++){          for(j=1;j<=i;j++){
               printf(" ");
          }
          for(k=n-1;k>=i;k--){
               printf("*");
          }
          printf("\n");
     }     return 0; }

7.Print a pattern
       *****        ****         ***          **            *          **         ***        ****      ***** 

#include<stdio.h>
int main(){     int i,j,k,n;     printf("Please enter the rows for the right andled triangle \n");     scanf("%d",&n);     for(i=n;i>=1;i--){          for(k=n;k>i;k--){
               printf(" ");
          }
          for(j=1;j<=i;j++){
               printf("*");
          }
          printf("\n");
     }     for(i=2;i<=n;i++){
          for(k=i;k<n;k++){
               printf(" ");
          }
          for(j=1;j<=i;j++){
               printf("*");
          }
          printf("\n");
     }
     return 0;}

8.Print a pattern
      *****      ****      ***      **      *      **      ***      ****      ***** 

#include<stdio.h>
int main(){     int i,j,n;     printf("Please enter the rows for the right andled triangle \n");     scanf("%d",&n);     for(i=n;i>=1;i--){          for(j=1;j<=i;j++){
               printf("*");
          }
          printf("\n");
     }     for(i=2;i<=n;i++){          for(j=1;j<=i;j++){
               printf("*");
          }
          printf("\n");
     }     return 0;}

9.Print a pattern 
      **********      ****  ****      ***    ***      **      **      *        *      **      **      ***    ***      ****  ****      ********** 

#include<stdio.h>/*calling the header files*/
int main(){     int i,j,k,n; /*intializing the variables which are needed*/     printf("Please enter the rows for the right andled triangle \n");     scanf("%d",&n);     /*Loop for the uppart part of the pattern*/     for(i=n;i>=1;i--){          for(j=1;j<=i;j++){
               printf("*");
          }
          for(k=n;k>i;k--){
               printf(" ");
          }
          for(j=1;j<=i;j++){
               printf("*");
          }          printf("\n");
     }     /*loop for lower part */     for(i=2;i<=n;i++){          for(j=1;j<=i;j++){
               printf("*");
          }
          for(k=i;k<n;k++){
               printf(" ");
          }
          for(j=1;j<=i;j++){
               printf("*");
          }
          printf("\n");
     }     return 0;}

10.Print a pattern
      ABCDEEDCBA      ABCD     DCBA      ABC         CBA      AB             BA      A                  A      AB             BA      ABC         CBA      ABCD     DCBA      ABCDEEDCBA 

#include<stdio.h>/*calling the header files*/
int main(){     int i,j,k,n,c,c1=0; /*intializing the variables which are needed*/     printf("Please enter the rows for the right andled triangle \n");     scanf("%d",&n);     /*Loop for the uppart part of the pattern*/     for(i=n;i>=1;i--){          c=64;          for(j=1;j<=i;j++){               printf("%c",c+j);          }          for(k=n;k>i;k--){               printf(" ");          }          c=65+n-c1;          for(j=1;j<=i;j++){               printf("%c",c-j);          }          c1++;          printf("\n");     }     /*loop for lower part */     c1=2;     for(i=2;i<=n;i++){          c=64;          for(j=1;j<=i;j++){               printf("%c",c+j);          }          for(k=i;k<n;k++){               printf(" ");          }          c=65+c1;          for(j=1;j<=i;j++){               printf("%c",c-j);          }          c1++;          printf("\n");     }     return 0;}

No comments:

Post a Comment