Thursday, December 31, 2015

C program to print various patterns

C program to print the below pattern
 ABCDEDCBA
  ABCDCBA
   ABCBA
    ABA
     A

#include <stdio.h>
void printme(int);

int main(void) {
  int n = 5;
  while(n){
    printme(n);
    n--;
  }
  return 0;
}

void printme(int n){
  int c = 65, i = 0;  // 65 is the ASCII value of A
  static int s = 0;  // To track the number of spaces to be printed
  s++;
  while(i<s){
    printf(" ");
    i++;
  }
  i = 0;
  while(i < n){
    printf("%c",c+i);
    i++;
  }
  i--;
  while(i--)
    printf("%c",c+i);
  printf("\n");
}

C program to print the below pattern
    A
   ABA
  ABCBA
 ABCDCBA
ABCDEDCBA

#include <stdio.h>
void printme(int);

int main(void) {
  int n = 5, i = 1, s = 0;
  while(i <= n){
    s = n-i; // Number of spaces to print before printing characters
    while(s--)
       printf(" ");
    printme(i);
    i++;
  }
  return 0;
}

void printme(int n){
  int c = 65, i = 0;  // 65 is the ASCII value of A
  while(i < n){
    printf("%c",c+i);
    i++;
  }
  i--;
  while(i--)
    printf("%c",c+i);
  printf("\n");
}


C program to print the below pattern
ABCDEEDCBA
ABCD**DCBA
ABC****CBA
AB******BA
A********A

#include <stdio.h>
void printme(int);

int main(void) {
  int n = 5, i = 1, s = 0;
  while(i <= n){
    printme(n);
    i++;
  }
  return 0;
}

void printme(int n){
  int c = 65, i = 0, temp;  // 65 is the ASCII value of A
  static int s = 0;
  while(i < (n-s)){
    printf("%c",c+i);
    i++;
  }
  temp = s*2;
  while(temp--){
    printf("*");
  }
  while(i--)
    printf("%c",c+i);
  printf("\n");
  s++;
}


More patterns will be updated soon

No comments:

Post a Comment