String Patterns in C

for loop
Print one line
#include <stdio.h>
#include <math.h>
int main(void){
char str[]="*";
int i;
for (i=1;i<=5;i++){
printf("%s",str);
}
return 0;
}
Output
*****

Print multiline with decreasing number of stars

#include <stdio.h>
#include <math.h>
int main(void){
char str[]="*";
int i=5;
int j;
for (i=5;i>=1;i--){
  for (j=1;j<=i;j++){
printf("%s",str);
  }
  printf("\n");
}
return 0;
}
Output is here
*****
****
***
**
*

Print multiline with increasing number of stars

#include <stdio.h>
#include <math.h>
int main(void){
char str[]="*";
int i=1;
int j;
for (i=1;i<=5;i++){
  for (j=1;j<=i;j++){
printf("%s",str);
  }
  printf("\n");
}
return 0;
}
*
**
***
****
*****

Conical pattern

#include <stdio.h>
#include <math.h>
int main(void){
char str[]="*";
int i=1;
int k=1;
int j;
for (i=1;i<=5;i++){
    for (k=i;k<=5;k++){
    printf(" ");// adding space before
    }
    for (j=1;j<=2*i-1;j++){
    printf("%s",str); // printing *
    }
    printf("\n");
}
return 0;
}
    *
   ***
  *****
 *******
********* 

Reverse Conical pattern

#include <stdio.h>
#include <math.h>
int main(void){
char str[]="*";
int i=1;
int k=1;
int j;
for (i=5;i>=0;i--){
    for (k=i;k<=5;k++){
    printf(" ");// adding space before
    }
    for (j=1;j<=2*i-1;j++){
    printf("%s",str); // printing *
    }
    printf("\n");
}
return 0;
}
*********
 *******
  *****
   ***
    *
In all the above codes you can display number in place of * .
printf("%d",i); 
Above line to be used in place of
printf("%s",str);
Output is here .
555555555
 4444444
  33333
   222
    1

One more pattern

#include <stdio.h>
int main(void){
   int i,j,k,blank;

   for(i=11;i>=0;i--){
if(i%2==0){
   for(j=1;j<=(i/2);j++){
    printf("*");
   }
j=j-1;
k=11-(2*j);
for(blank=1;blank<=k;blank++){
 printf("#");
}
for(j=1;j<=(i/2);j++){
    printf("*");
 }
 printf("   i=%d, i=%d,j=%d,k=%d blank=%d\n",i,i/2,j,k,blank);
}
}
return 0;
}
*****#*****   i=10, i=5,j=6,k=1 blank=2
****###****   i=8, i=4,j=5,k=3 blank=4
***#####***   i=6, i=3,j=4,k=5 blank=6
**#######**   i=4, i=2,j=3,k=7 blank=8
*#########*   i=2, i=1,j=2,k=9 blank=10
###########   i=0, i=0,j=1,k=11 blank=12
Your Rating




Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2019 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer