Sie sind auf Seite 1von 14

Write a C program to print the following pattern: 1.

BA

CBA

DCBA

EDCBA

Sol:
#include <stdio.h> int main() { char prnt = 'A'; int i, j; for (i = 1; i <= 5; i++) { // Prints the right angle triangle for (j = 1; j <= i; j++) { printf("%2c", ((prnt + (i - j)))); // Prints the characters in the required order } printf("\n"); } return 0; }

2.

ABCDE

BCDE

CDE

DE

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

char c = 'A'; for (i = 0; i < 5; i++) { for (j = 0; j < 5; j++) { if (j < i) { printf(" "); } else { printf("%c", c + j); are increasing in terms of j } printf(" "); } printf("\n"); } return 0; }

// The order of the alphabets

3.

a a

B B B

b b b b

C C C C C

c c c c

D D D

d d

Sol: #include<stdio.h>

int main() { char prnt_even = 'a'; char prnt_odd = 'A'; int i, j, s, nos = 4,line=0; //line tracks if the row is odd or even for (i = 1; i <= 9; (i = i + 2)) { for (s = nos; s >= 1; s--) { // For the required spacings printf(" "); } for (j = 1; j <= i; j++) {

if(line==0){ if ((i % 2) != 0 && (j % 2) != 0) { printf("%2c", prnt_odd); // Prints capital letter if l=0 } else { printf(" "); } } else{ if ((i % 2) != 0 && (j % 2) != 0) { printf("%2c", prnt_even); //else prints a small letter } else { printf(" "); } } } /* After one column the value of line is interchanged and prnt_even or prnt_odd is incremented accordingly */ if(line==0){ prnt_odd++; line=1; } else{ prnt_even++; line=0; } nos--; //no of space decremented by 1 printf("\n"); } nos = 1; /* Second half of the diamond. This one skips its first row rest goes pretty much the same */ for (i = 7; i >= 1; (i = i - 2)) { for (s = nos; s >= 1; s--) { printf(" "); } for (j = 1; j <= i; j++) { if(line==0){ if ((i % 2) != 0 && (j % 2) != 0) { printf("%2c", prnt_odd); } else { printf(" "); } } else{ if ((i % 2) != 0 && (j % 2) != 0) { printf("%2c", prnt_even); } else { printf(" "); } } } if(line==0){ prnt_odd++; line=1; } else{ prnt_even++; line=0; }

nos++; //no of spaces is incremented by one printf("\n"); } return 0; } Explanation: This is a diamond formation where all the even places are hollowed. In case of odd row no a capital letter is printed and a small case alphabet otherwise. Both the capital letters n the small letters increment by one starting from A or a.

4.A

B C

Y X

D E F

W V U

G H I J

T S R Q

K L M N O

Sol: #include <stdio.h>

int main() { char prnt_odd = 'A'; char prnt_even = 'Z'; int i, j; // The right angle has nine rows for (i = 1; i <= 9; i++) { for (j = 1; j <= i; j++) { //checks for the column printing restrictions.. if ((i % 2) != 0 && (j % 2) != 0) { printf("%2c", prnt_odd); prnt_odd++; } else if ((i % 2) == 0 && (j % 2) == 0) { printf("%2c", prnt_even); prnt_even--; } else { printf(" ");

} return 0;

} } printf("\n");

Explanation: This is a right angle triangle of alphabets, having two sets of alphabets. One for the odd rows which starts from A and increments by one. And another one for the even rows which starts from Z and decrements by 1. The restriction in the columns are: if the row no is odd only the odd places are printed else if the row no is even only the even places are filled, rest are skipped.

5. A B C D E

Z Y X W

A B C D

Z Y X

A B C

Z Y

A B

A B

Z Y

A B C

Z Y X

A B C D

Z Y X W

A B C D E

Sol: #include <stdio.h>


//This prints the columns with guidance of the row number supplied. int triangle(int i) { char prnt_odd = 'A'; char prnt_even= 'Z'; int j; for(j=1; j<=i; j++) { //Checks for the restrictions if((i%2)!=0 && (j%2)!=0) { printf("%2c",prnt_odd); prnt_odd++; } else if((i%2)==0 && (j%2)==0) { printf("%2c",prnt_even); prnt_even--; } else { printf(" "); } } return 0; } int main() { int i; //Prints the first right angle triangle with 9 rows for(i=9; i>=1; i--) { triangle(i); printf("\n"); } // This prints the second right angle triangle skipping its tip. for(i=2; i<=9; i++) { triangle(i); printf("\n"); } return 0; }

Explanation: This pattern can be seen as two right angle triangles of alphabets, having the same tip with two sets of alphabets. One for the odd rows which starts from A and increments by one. And another one for the even rows which starts from Z and decrements by 1. The restriction in the columns are: if the row number is odd, only the odd places are printed. Else if the row number is even only the even places are filled, rest are skipped.

ABCDEDCBA

ABCD DCBA

ABC

CBA

AB

BA

AB

BA

ABC

CBA

ABCD DCBA

ABCDEDCBA

Sol: #include <stdio.h>


/* nos = Number of Spaces required. i = Number of columns skip = The row in which we have to skip the first character form = Whether the alphabets has to printed in Incremental or Decremental order */ int triangle(int nos, int i, int skip, int form) { char prnt[] = "ABCDE"; int j, s; for (s = nos; s >= 1; s--) { // Controls the required spacings printf(" "); } for (j = 0; j <= i; j++) { // prints the characters row wise if (skip != 1) { // If we need to skip a row. if (i == skip && j == 0) { continue; } }

if (form == 0) { /* Decides whether the characters are in incremental or decremental order. */ printf("%2c", prnt[j]); } else { printf("%2c", prnt[i - j]); } } return 0; } int main() { int i, nos = -1; for (i = 4; i >= 0; i--) { triangle(0, i, 1, 0); // Prints the first triangle triangle(nos, i, 4, 1); // Prints the second triangle nos = nos + 2; // Spacing factor printf("\n"); } nos = 5; for (i = 1; i <= 4; i++) { triangle(0, i, 0, 0); // Prints the third triangle triangle(nos, i, 4, 1); // Prints the fourth triangle nos = nos - 2; //Spacing factor. printf("\n"); } return 0; }

Explanation: This pattern can be treated as four individual right angle triangles composed of alphabets in a sequential manner.

7. A

BCD

EFGHI

JKLMNOP

QRSTUVWXY

YXWVUTSRQ

PONMLKJ

IHGFE

DCB

Sol: #include <stdio.h>


int main() { char prnt = 'A'; int i, j, s, nos = 5; for (i = 1; i <= 9; (i = i + 2)) { for (s = nos; s >= 1; s--) { printf(" "); } for (j = 1; j <= i; j++) { printf("%2c", prnt); ++prnt; //Increments the alphabet if (i == 9 && j == 9) //for the extra Z { printf("\f%2c", prnt); } }

} printf("\f"); //Line Feed for (i = 9; i >= 1; (i = i - 2)) { //Maintaining the required Spaces if (i == 9) { nos = 0; } else if (i == 7) { nos = 12; } for (s = nos; s >= 1; s--) { printf(" "); } for (j = 1; j <= i; j++) { --prnt; //decrements the alphabet before printing it. printf("%2c", prnt); } nos++; printf("\n"); } return 0;

nos--; //for the continuation if (i != 9) { printf("\n"); }

Explanation: This can be seen as two isosceles triangle made up of alphabets incrementing by one starting from A. The connecting Z can be considered additionally.

NN N

N NN

Sol: #include <stdio.h>

int main() { char prnt = 'N'; int i, j, k; for (i = 1; i <= 4; i++) { //First right-angle triangle. for (j = 1; j <= i; j++) { if (j == 1 || j == i) { // This Ensures an empty baseless triangle printf("%2c", prnt); //Prints the character after 2 spaces. } else { printf(" "); } }

//Second right-angle triangle for (k = 3; k >= i; k--) { if (k == i) { // k=0 has to skipped as it shares the same hypotenuse if (i == 4) { //For the joining point break; } printf("%2c", prnt); } else { printf(" "); } } printf("\n"); } return 0; }

Explanation: This pattern can be viewed as two baseless right-angle triangles arranged in such a way that they share a common hypotenuse

X X

X X

Sol: #include <stdio.h>

int main() { char prnt = 'X'; int i, j, s, nos = 0; //nos controls the spacing. //1st triangle for (i = 9; i >= 1; (i = i - 2)) { for (s = nos; s >= 1; s--) { //Spacing control

} /* Since both the triangles have the the second triangle we'll ignore its and the nos from 3.*/ nos = 3; for (i = 3; i <= 9; (i = i + 2)) for (s = nos; s >= 1; s--) { printf(" "); } for (j = 1; j <= i; j++) { if (j == 1 || j == i) { printf("%2c", prnt); } else { printf(" "); } } printf("\n"); nos--; //The spaces are in a } return 0; }

printf(" "); } for (j = 1; j <= i; j++) { if (j == 1 || j == i) { //This hollows the triangle printf("%2c", prnt); } else { printf(" "); } } printf("\n"); nos++; // In the upper triangle the space increments by 1. same peak point, while printing peak point. Thus i starts from 3 {

decrementing order.

K K

KK

KK

K K

Sol: #include <stdio.h>


int main() { char prnt = 'K'; int i, j; //First right-angle triangle

for (i = 7; i >= 1; i--) { for (j = 1; j <= i; j++) { if (j == 1 || j == i) { //This hollows the triangle printf("%2c", prnt); } else { printf(" "); } } if (i != 1) { printf("\n"); } } // Second triangle for (i = 1; i <= 7; i++) { for (j = 1; j <= i; j++) { if (j == 1 || j == i) { printf("%2c", prnt); } else { printf(" "); } } printf("\n"); } return 0; }

Explanation: This pattern can be be seen as an inverted right triangle placed over a normal rightangled triangle. In addition to the triangles, there is an extra character in the intersection point. Since both the triangles share the same tip and in the figure and we have an extra character in the point of intersection, we can start the second right angle triangle from the same line where the first one ends.

Das könnte Ihnen auch gefallen