You are on page 1of 12

1.

Write a C program to print the following pattern: A B C D E

A B A C B A D C B A

2. Write a C program to print the following pattern: A B C D E B C D E C D E D E E

3. Write a C program to print the following pattern: A a B b C c D d E a B b C c D d B b C c D b C c

4. Write a C program to print the following pattern: A Z B Y D W G T K

C X E V H S L

F U I R M

J Q N

5. Write a C program to print the following pattern: A Z A Z A Z A Z A Z A Z A Z A Z Y B Y B Y B B X C X C C W D D E

B Y B Y B Y

C X C X

D W

6. Write a C program to print the following pattern: A A A A A A A A A B C D E D C B B C D D C B B C C B B B B B C C B C D D C B C D E D C B B B B A A A A A A A A A

7. Write a C program to print the following pattern: B E J Q Z Y P I D A C F K R X O H C A D G H I L M N O P S T U V W X Y W V U T S R Q N M L K J G F E B

8. Write a C program to print the following pattern: N N N N N N N N N N 9. Write a C program to print the following pattern: X X X X X X X X X X X X X X X X X

10. Write a C program to print the following pattern: K K K K K K K K K K K K K K K K K K K

K K K K K

K K K K K

1. Write a C program to print the following pattern: A B C D E A B A C B A D C B A

Program: view source print? 01 #include <stdio.h> 02 03 int main() { 04 char prnt = 'A'; 05 int i, j; 06 for (i = 1; i <= 5; i++) { // Prints the right angle triangl e 07 for (j = 1; j <= i; j++) { 08 printf("%2c", ((prnt + (i - j)))); // Prints the ch aracters in the required order 09 } 10 printf("\n"); 11 } 12 return 0; 13 }

2. Write C program to print the following pattern: A B C D B C D C D D E E E E E

Program: view source print? 01 #include<stdio.h> 02 int main() { 03 int i, j; 04 char c = 'A'; 05 for (i = 0; i < 5; i++) { 06 for (j = 0; j < 5; j++) { 07 if (j < i) { 08 printf(" "); 09 } else { 10 printf("%c", c + j); // The order of the alphab ets are increasing in terms of j 11 } 12 printf(" "); 13 }

Bac

to top

14 15 16 17

printf("\n"); } return 0; } to top

3. Write C program to print the following pattern: A a B b C c D d E Program: view source print? 01 #include<stdio.h> 02 int main() { 03 char prnt_even = 'a'; 04 char prnt_odd = 'A'; 05 int i, j, s, nos = 4,line=0; //line trac s if the row is od d or even 06 for (i = 1; i <= 9; (i = i + 2)) { 07 for (s = nos; s >= 1; s--) { // For the required spacin gs 08 printf(" "); 09 } 10 for (j = 1; j <= i; j++) { 11 if(line==0){ 12 if ((i % 2) != 0 && (j % 2) != 0) { 13 printf("%2c", prnt_odd); // Prints capital letter if l=0 14 } else { 15 printf(" "); 16 } 17 } 18 else{ 19 if ((i % 2) != 0 && (j % 2) != 0) { 20 printf("%2c", prnt_even); //else prints a small letter 21 } else { 22 printf(" "); 23 } 24 } 25 } 26 /* After one column the value of line is interchanged and prnt_e ven or prnt_odd is incremented accordingly */ 27 if(line==0){ 28 prnt_odd++; 29 line=1; 30 } 31 else{ 32 prnt_even++; 33 line=0; C c D d b C c D B b C c a B b C

Bac

34 } 35 nos--; //no of space decremented by 1 36 printf("\n"); 37 } 38 nos = 1; 39 /* Second half of the diamond. This one s ips its first row rest goes pretty much the same */ 40 for (i = 7; i >= 1; (i = i - 2)) { 41 for (s = nos; s >= 1; s--) { 42 printf(" "); 43 } 44 for (j = 1; j <= i; j++) { 45 if(line==0){ 46 if ((i % 2) != 0 && (j % 2) != 0) { 47 printf("%2c", prnt_odd); 48 } else { 49 printf(" "); 50 } 51 } 52 else{ 53 if ((i % 2) != 0 && (j % 2) != 0) { 54 printf("%2c", prnt_even); 55 } else { 56 printf(" "); 57 } 58 } 59 } 60 if(line==0){ 61 prnt_odd++; 62 line=1; 63 } 64 else{ 65 prnt_even++; 66 line=0; 67 } 68 nos++; //no of spaces is incremented by one 69 printf("\n"); 70 } 71 return 0; 72 } Explanation: This is a diamond formation where all the even places are hollowed. In cas e 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 Bac to top 4. Write a C program to print the following pattern: A Z B Y D W G T K L H S M E V I R N C X F U J Q O

Program:

a .

view source print? 01 #include <stdio.h> 02 int main() { 03 char prnt_odd = 'A'; 04 char prnt_even = 'Z'; 05 int i, j; 06 // The right angle has nine rows 07 for (i = 1; i <= 9; i++) { 08 for (j = 1; j <= i; j++) { 09 //chec s for the column printing restrictions.. 10 if ((i % 2) != 0 && (j % 2) != 0) { 11 printf("%2c", prnt_odd); 12 prnt_odd++; 13 } else if ((i % 2) == 0 && (j % 2) == 0) { 14 printf("%2c", prnt_even); 15 prnt_even--; 16 } else { 17 printf(" "); 18 } 19 } 20 printf("\n"); 21 } 22 return 0; 23 } 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 f or the even rows which starts from Z and decrements by 1. The restriction in the c olumns 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 s ipped. Bac to top 5. Write a C program to print the following pattern: A Z A Z A Z A Z A Z A Z A Z A Z A B B Y C B Y C X D B Y C X D W E B B Y B Y C B Y C X C X D D W E

Program: view source print? 01 #include <stdio.h> 02 //This prints the columns with guidance of the row number suppli ed.

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 s ipping its for(i=2; i<=9; i++) { triangle(i); printf("\n"); } return 0; }

tip. 39 40 41 42 43 44 45

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, o nly the odd places are printed. Else if the row number is even only the even pla ces are filled, rest are s ipped. Bac to top 6. Write a C program to print the following pattern: A A A A B C D E D C B B C D D C B B C C B B B A A A A

03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

int triangle(int i) { char prnt_odd = 'A'; char prnt_even= 'Z'; int j; for(j=1; j<=i; j++) { //Chec s 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; }

A A A A A

B B C C B C D D C B C D E D C

B B B B

A A A A A

Program: view source print? 01 #include <stdio.h> 02 /* 03 nos = Number of Spaces required. 04 i = Number of columns 05 s ip = The row in which we have to s ip the first character 06 form = Whether the alphabets has to printed in Incremental or Decremental order 07 */ 08 int triangle(int nos, int i, int s ip, int form) { 09 char prnt[] = "ABCDE"; 10 int j, s; 11 for (s = nos; s >= 1; s--) { // Controls the required spac ings 12 printf(" "); 13 } 14 for (j = 0; j <= i; j++) { // prints the characters row wise 15 if (s ip != 1) { // If we need to s ip a row. 16 if (i == s ip && j == 0) { 17 continue; 18 } 19 } 20 if (form == 0) { /* Decides whether the characters are i n incremental or decremental order. */ 21 printf("%2c", prnt[j]); 22 } else { 23 printf("%2c", prnt[i - j]); 24 } 25 } 26 return 0; 27 } 28 29 int main() { 30 int i, nos = -1; 31 for (i = 4; i >= 0; i--) { 32 triangle(0, i, 1, 0); // Prints the first triangle 33 triangle(nos, i, 4, 1); // Prints the second triangle 34 nos = nos + 2; // Spacing factor 35 printf("\n"); 36 } 37 nos = 5; 38 for (i = 1; i <= 4; i++) { 39 triangle(0, i, 0, 0); // Prints the third triangle 40 triangle(nos, i, 4, 1); // Prints the fourth triangle 41 nos = nos - 2; //Spacing factor. 42 printf("\n"); 43 } 44 return 0; 45 } Explanation:

This pattern can be treated as four individual right angle triangles compo sed of alphabets in a sequential manner. Bac to top 7. Write a C program to print the following pattern: B E F J K L Q R S T A C G M U D H I N O P V W X Y Z Y X W V P O N I H D U M G C A T S R Q L K J F E B

Program: view source print? 01 #include <stdio.h> 02 int main() { 03 char prnt = 'A'; 04 int i, j, s, nos = 5; 05 for (i = 1; i <= 9; (i = i + 2)) { 06 for (s = nos; s >= 1; s--) { 07 printf(" "); 08 } 09 for (j = 1; j <= i; j++) { 10 printf("%2c", prnt); 11 ++prnt; //Increments the alphabet 12 if (i == 9 && j == 9) //for the extra Z 13 { 14 printf("\f%2c", prnt); 15 } 16 } 17 nos--; 18 //for the continuation 19 if (i != 9) { 20 printf("\n"); 21 } 22 } 23 printf("\f"); //Line Feed 24 for (i = 9; i >= 1; (i = i - 2)) { 25 //Maintaining the required Spaces 26 if (i == 9) { 27 nos = 0; 28 } else if (i == 7) { 29 nos = 12; 30 } 31 for (s = nos; s >= 1; s--) { 32 printf(" "); 33 } 34 for (j = 1; j <= i; j++) { 35 --prnt; //decrements the alphabet before printing it. 36 37 38 printf("%2c", prnt); } nos++;

39 40 41 42

printf("\n"); } return 0; }

Explanation: This can be seen as two isosceles triangle made up of alphabets incrementi ng by one starting from A . The connecting Z can be considered additionally. Bac to top 8. Write a C program to print the following pattern: N N N N N N N N N N Program: view source print? 01 #include <stdio.h> 02 int main() { 03 char prnt = 'N'; 04 int i, j, ; 05 for (i = 1; i <= 4; i++) { 06 //First right-angle triangle. 07 for (j = 1; j <= i; j++) { 08 if (j == 1 || j == i) { // This Ensures an empty baseless triangle 09 printf("%2c", prnt); //Prints the character af ter 2 spaces. 10 } else { 11 printf(" "); 12 } 13 } 14 //Second right-angle triangle 15 for ( = 3; >= i; --) { 16 if ( == i) { // =0 has to s ipped as it shares the same hypotenuse 17 if (i == 4) { //For the joining point 18 brea ; 19 } 20 printf("%2c", prnt); 21 } else { 22 printf(" "); 23 } 24 } 25 printf("\n"); 26 } 27 return 0; 28 } Explanation: This pattern can be viewed as two baseless right-angle triangles arranged in such a way that they share a common hypotenuse. Bac to top 9. Write a C program to print the following pattern: X X

X X X X X X X X X X X X

X X

Program: view source print? 01 #include <stdio.h> 02 int main() { 03 char prnt = 'X'; 04 int i, j, s, nos = 0; //nos controls the spacing. 05 //1st triangle 06 for (i = 9; i >= 1; (i = i - 2)) { 07 for (s = nos; s >= 1; s--) { //Spacing control 08 printf(" "); 09 } 10 for (j = 1; j <= i; j++) { 11 if (j == 1 || j == i) { //This hollows the triangle 12 printf("%2c", prnt); 13 } else { 14 printf(" "); 15 } 16 } 17 printf("\n"); 18 nos++; // In the upper triangle the space increments by 1. 19 } 20 /* Since both the triangles have the same pea point, while prin ting the second triangle we'll ignore its pea point. Thus i starts from 3 and t he nos from 3.*/ 21 nos = 3; 22 for (i = 3; i <= 9; (i = i + 2)) { 23 for (s = nos; s >= 1; s--) { 24 printf(" "); 25 } 26 for (j = 1; j <= i; j++) { 27 if (j == 1 || j == i) { 28 printf("%2c", prnt); 29 } else { 30 printf(" "); 31 } 32 } 33 printf("\n"); 34 nos--; //The spaces are in a decrementing order. 35 } 36 return 0; 37 } Bac to top 10. Write a C program to print the following pattern: K K K K K K

K K K K K K K K K K K K K K K K K K K K K K K Program: view source print? 01 #include <stdio.h> 02 int main() { 03 char prnt = 'K'; 04 int i, j; 05 //First right-angle triangle 06 for (i = 7; i >= 1; i--) { 07 for (j = 1; j <= i; j++) { 08 if (j == 1 || j == i) { //This hollows the triangle 09 printf("%2c", prnt); 10 } else { 11 printf(" "); 12 } 13 } 14 if (i != 1) { 15 printf("\n"); 16 } 17 } 18 // Second triangle 19 for (i = 1; i <= 7; i++) { 20 for (j = 1; j <= i; j++) { 21 if (j == 1 || j == i) { 22 printf("%2c", prnt); 23 } else { 24 printf(" "); 25 } 26 } 27 printf("\n"); 28 } 29 return 0; 30 } Explanation: This pattern can be be seen as an inverted right triangle placed over a no rmal right-angled triangle. In addition to the triangles, there is an extra char acter 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 a ngle triangle from the same line where the first one ends.

You might also like