String Patterns in C

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 #2

#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 #3

#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 #4

#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 # 5

#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 #6

#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

pattern #7

#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

pattern # 8

Only change in above code is highlighted.
#include <stdio.h>
int main(void){
   int i,j,k,blank;

for(i=1;i<=11;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=2, i=1,j=2,k=9 blank=10
**       **   i=4, i=2,j=3,k=7 blank=8
***     ***   i=6, i=3,j=4,k=5 blank=6
****   ****   i=8, i=4,j=5,k=3 blank=4
***** *****   i=10, i=5,j=6,k=1 blank=2

pattern # 9

This will combine above two patterns ( # 7 & # 8)
#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);
}
}

///////
for(i=1;i<=11;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
*         *   i=2, i=1,j=2,k=9 blank=10
**       **   i=4, i=2,j=3,k=7 blank=8
***     ***   i=6, i=3,j=4,k=5 blank=6
****   ****   i=8, i=4,j=5,k=3 blank=4
***** *****   i=10, i=5,j=6,k=1 blank=2
Create number series like this
1,2,3,4,5,6,7,8,9
1,3,5,7,9
1,4,7
1,5,9
1,6
1,7
1,8
1,9
1
Solution
#include <stdio.h>

int main()
{
    for(int i=1; i<10; i++){
        for (int j=1;j<10; j=j+i){
            printf(" %d ,",j);
       }
    printf("\n");
}

    return 0;
}

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com






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