Sunteți pe pagina 1din 58

Number problems in C

# Write C program to print the following pattern:


1 22 333 4444 55555
Program: view source print? 01 02 03 04 05 06 07 08 09 10 11 12 13 } for (j = 1;
j <= i; j++) { /* %2d ensures that the number is printed in two spaces for alig
nment and the numbers are printed in the order. */ printf("%2d", i); #include<st
dio.h> int main() { int i, j, k, c = 5; for (i = 1; i <= 5; i++) { /* k is taken
for spaces */ for (k = 1; k <= c; k++) { /* blank space */ printf(" ");

14 15 16 17 18 19 20 } }
} printf("\n"); /*c is decremented by 1 */ c--;
return 0;
Download Code
Output:
1 22 333 4444 55555

Explanation: Here i loop is used for printing the numbers in the respective rows a
nd k loop is used for providing spaces. j loop prints the numbers. c is decremented f
r numbers to be displayed in alternate columns.
Back to top
# Write C program to print the following pattern:
1

121 12321 1234321 123454321


Program: view source print? 01 02 03 04 05 06 07 08 09 } printf("%2d", j); } #in
clude<stdio.h> int main() { /* c taken for columns */ int i, j, c = 9, m, k; for
(i = 1; i <= 5; i++) { /* k is used for spaces */ for (k = 1; k <= c; k++) { pr
intf(" ");
10 for (j = 1; j <= i; j++) { for (m = j - 2; m > 0; m--) { 11 12 13 14 15 16 17
18 } printf("\n"); /* c is decremented by 2 */ c = c - 2;
/* %2d ensures that the number * is printed in two spaces * for alignment */ pri
ntf("%2d", m);

19 20 21 }
} return 0;
Download Code
Output:
1 121 12321 1234321 123454321
Explanation: Here i loop is used for printing numbers in rows and k loop is used for
providing spaces. j loop is used for printing numbers in increasing order. m loop i
s used for printing numbers in reverse order.
Back to top
# Write a C program to display the following format:
-----ab -----15 24

33 42 51 -----Program: view source print? 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 1


8 } printf("----------"); #include<stdio.h> int main() { int i = 1, j = 5; print
f("----------\n"); printf("a \t b \n"); printf("----------\n"); /* logic: while
loop repeats * 5 times i.e. until * the condition i<=5 fails */ while (i <= 5) {
/* i and j value printed */ printf("%d \t %d\n", i, j); /* i and j value increm
ented by 1 for every iteration */ i++; j--;

19 20 }
return 0;
Download Code
Output:
-----ab -----15 24 33 42 51 -----Explanation: Here, i is initialized to least value 1 and j initialized to highest va
lue 5. We keep incrementing the i value and decrementing the j value until the cond
ition fails. The value is displayed at each increment and at each decrement. Bac
k to top
# Write a C program to display the following format:
-------no. sum --------

1 1 2 3 3 6 4 10 5 15 -------Program: view source print? 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 #inc


lude<stdio.h> int main() { int num = 1, sum = 0; printf("-----------\n"); printf
("num \t sum\n"); printf("-----------\n"); /* while loop repeats 5 times * i.e.
until the condition * num <= 5 fails */ while (num <= 5) { sum = sum + num; prin
tf("%d \t %d\n", num, sum); /* num incremented by 1 * for every time * the loop
is executed */ num++;

17 18 19 20 }
} printf("-----------"); return 0;
Download Code
Output:
-------no. sum -------1 1 2 3 3 6 4 10 5 15 --------

Explanation: In the above program we have taken two variables num and sum. num is use
to check the condition and to display the numbers up to 5. sum is used to add the
numbers which are displayed using variable num. The sum value is initialized to zer
o. sum is added to the numbers which are incremented by i and displayed.
10 most challanging pattern problems in C
1. Write a C program to print the following pattern:

* * * * * * * * * *
2. Write a C program to print the following pattern:
* ***
* ***
***** ***** ***********
3. Write a C program to print the following pattern:
* * * * * * * * * * * * * * * * * * * * * * * *
*
*
*
*
*
4. Write a C program to print the following pattern:

* * * * * * * * * * * * * * * * * * * * * * * * * * * * *
5. Write a C program to print the following pattern:
* *** ***** ******* ********* ******* ***** *** * *** *****
6. Write a C program to print the following pattern:

* ** *** **** *** ** *


7. Write a C program to print the following pattern:
********* **** **** *** ** * ** *** *** ** * ** ***
**** **** *********
8. Write a C program to print the following pattern:
***************** ******* ******* ***** *****

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

************************* * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * *
*************************
1. Write a C program to print the following pattern:
* * * * * * * * * *
Program: view source print? 01/* This is a simple mirror-image of a right angle
triangle */ 02 03#include <stdio.h> 04int main() { 05 char prnt = * ;

06 int i, j, nos = 4, s; 07 for (i = 1; i <= 5; i++) { 08for (s = nos; s >= 1; s


--) { // Spacing factor 09 printf(" ");
10 } 11 for (j = 1; j <= i; j++) { 12 printf("%2c", prnt);
13 } 14 printf("\n"); 15 --nos; // Controls the spacing factor 16 } 17 return 0;
18}
Download Code
Back to top
2. Write C program to print the following pattern:
* ***
* ***
***** ***** ***********
Program:

view source print? 01#include<stdio.h> 02int main() { 03 char prnt = * ; 04 int


i, j, k, s, c = 1, nos = 9; 05 for (i = 1; c <= 4; i++) { 06 // As we want to p
rint the columns in odd sequence viz. 1,3,5,.etc
07 if ((i % 2) != 0) { 08 for (j = 1; j <= i; j++) {
09 printf("%2c", prnt);
12 13 14 15 16 17 18 19
"); } for (k = 1; k <=
prnt); } printf("\n");

10} 11for (s = nos; s >= 1; s--) { //The spacing factor


20 21 22 23 24 if (c == 4 && s == 1) { break; } printf("
i; k++) { if (c == 4 && k == 5) { break; } printf("%2c",
nos = nos - 4; // controls the spacing factor

25
++c;
26 } 27 } 28 return 0; 29}
Download Code
Back to top
3. Write C program to print the following pattern:
* * * * * * * * * * * * * * * * * * * * * * * *
*
*
*
*
*
Program: view source print? 01#include<stdio.h>

02int main() { 03 char prnt = * ; 04 int i, j, k, s, p, r, nos = 7; 05 06 for (


i = 1; i <= 5; i++) { 07 for (j = 1; j <= i; j++) { 08 if ((i % 2) != 0 && (j %
2) != 0) { 09printf("%3c", prnt); 10} 11else if ((i % 2) == 0 && (j % 2) == 0) {
12printf("%3c", prnt); 13} 14else { 15printf(" "); 16} 17} 18for (s = nos; s >=
1; s--) { // for the spacing factor 19 printf(" ");
20 } 21 for (k = 1; k <= i; k++) { //Joining seperate figures 22if (i == 5 && k
== 1) { 23 continue; 24} 25if ((k % 2) != 0) { 26printf("%3c", prnt); 27}

28else { 29printf(" "); 30} 31} 32printf("\n"); 33nos = nos - 2; // space contro
l 34} nos = 1; // remaining half.. 35for (p = 4; p >= 1; p--) { 36 for (r = 1; r
<= p; r++) { 37if ((p % 2) != 0 && (r % 2) != 0) { 38printf("%3c", prnt); 39} 4
0else if ((p % 2) == 0 && (r % 2) == 0) { 41printf("%3c", prnt); 42} 43else { 44
printf(" "); 45} 46} 47for (s = nos; s >= 1; s--) { 48 printf(" ");
49 } 50 for (k = 1; k <= p; k++) { 51 52 53 if ((k % 2) != 0) { printf("%3c", pr
nt); }

54else { 55 56 printf(" "); }


57 } 58 nos = nos + 2; // space control 59 printf("\n"); 60 } 61 return 0; 62}
Download Code
Explanation: This can be seen as an inverted diamond composed of stars. It can b
e noted that the composition of this figure follows sequential pattern of consec
utive stars and spaces.
In case of odd row number, the odd column positions will be filled up with *, else
a space will be spaced and vice-versa in case of even numbered row.
In order to achieve this we will construct four different right angle triangles
aligned as per the requirement. Back to top
4. Write a C program to print the following pattern:

* * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Program: view source print? 01#include<stdio.h> 02int main() { 03 char prnt = *
; 04 int i, j, s, nos = 0; 05 for (i = 9; i >= 1; (i = i - 2)) { 06 for (s = no
s; s >= 1; s--) { 07 printf(" ");
08 } 09 for (j = 1; j <= i; j++) { 10 11 12 13 if ((i % 2) != 0 && (j % 2) != 0)
{ printf("%2c", prnt); } else { printf(" ");

14
}
15 } 16 printf("\n"); 17 nos++; 18 } 19 nos = 3; 20 for (i = 3; i <= 9; (i = i +
2)) { 21for (s = nos; s >= 1; s--) { 22 printf(" ");
23 } 24 for (j = 1; j <= i; j++) { 25 26 27 28 29 30 if ((i % 2) != 0 && (j % 2)
!= 0) { printf("%2c", prnt); } else { printf(" "); }
31 } 32 nos--; 33 printf("\n"); 34 } 35 return 0; 36}
Download Code Back to top

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


* *** ***** ******* ********* ******* ***** *** * *** *****
Program: view source print? 01#include<stdio.h> 02int main() { 03 char prnt = *
; 04 int i, j, k, s, nos = 4; 05 for (i = 1; i <= 5; i++) { 06for (s = nos; s >
= 1; s--) { 07 printf(" ");
08 }

09 for (j = 1; j <= i; j++) { 10 printf("%2c", prnt);


11 } 12 for (k = 1; k <= (i - 1); k++) { 13if (i == 1) { 14} 15printf("%2c", prn
t); 16} 17 printf("\n"); nos--; 18} 19 nos = 1; 20for (i = 4; i >= 1; i--) { 21
for (s = nos; s >= 1; s--) { 22 printf(" "); continue;
23 } 24 for (j = 1; j <= i; j++) { 25 printf("%2c", prnt);
26 } 27 for (k = 1; k <= (i - 1); k++) { 28 printf("%2c", prnt);
29 } 30 nos++; 31 printf("\n"); 32 } 33 nos = 3; 34 for (i = 2; i <= 5; i++) {

35if ((i % 2) != 0) { 36for (s = nos; s >= 1; s--) { 37 38 39 40 41 printf(" ");


} for (j = 1; j <= i; j++) { printf("%2c", prnt); }
42 } 43 if ((i % 2) != 0) { 44 45 printf("\n"); nos--;
46 } 47 } 48 return 0; 49}
Download Code Back to top
6. Write a C program to print the following pattern:
* ** *** **** ***

** *
Program: view source print? 01/* 02 03 04*/ 05#include <stdio.h> 06// This funct
ion controls the inner loop and the spacing 07// factor guided by the outer loop
index and the spacing index. 08int triangle(int nos, int i) { 09 char prnt = *
; 10 int s, j; 11 for (s = nos; s >= 1; s--) { 12 printf(" "); 13 } 14 for (j =
1; j <= i; j++) { 15 printf("%2c", prnt); 16 } 17 return 0; 18} 19 20int main()
{ //The inner loop // Spacing factor This can be seen as two right angle triang
les sharing the same base which is modified by adding few extra shifting spaces

21 int i, nos = 5; 22 //draws the upper triangle 23 for (i = 1; i <= 4;


4 triangle(nos, i); 25 nos++; 26 printf("\n"); } 27nos = 7; //Draws the
iangle skipping its base. 28for (i = 3; i >= 1; i--) { 29 int j = 1; 30
(nos, i); // Inner loop construction 31 nos = nos - j; 32 printf("\n");
return 0; 35} // Spacing factor //Inner loop construction // Increments
ing factor
Download Code Back to top
7. Write a C program to print the following pattern:
********* **** **** *** ** * *** ** *

i++) { 2
lower tr
triangle
33 } 34
the spac

** ***
** ***
**** **** *********
Program: view source print? 01#include <stdio.h> 02 03int main() { 04 char prnt
= * ; 05 int i, j, k, s, nos = -1; 06 for (i = 5; i >= 1; i--) { 07 for (j = 1;
j <= i; j++) { 08 printf("%2c", prnt); 09} 10for (s = nos; s >= 1; s--) { 11 pr
intf(" ");
12 } 13 for (k = 1; k <= i; k++) { 14 15 16 17 if (i == 5 && k == 5) { continue;
} printf("%2c", prnt);
18 }

19 nos = nos + 2; 20 printf("\n"); 21 } 22 nos = 5; 23 for (i = 2; i <= 5; i++)


{ 24 for (j = 1; j <= i; j++) { 25 printf("%2c", prnt); 26} 27for (s = nos; s >=
1; s--) { 28 printf(" ");
29 } 30 for (k = 1; k <= i; k++) { 31 32 33 34 if (i == 5 && k == 5) { break; }
printf("%2c", prnt);
35 } 36 nos = nos - 2; 37 printf("\n"); 38 } 39 return 0; 40}
Download Code Back to top

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


***************** ******* ******* ***** *** ***** ***
********* ******* ***** *** *
Program: view source print? 01#include <stdio.h> 02int main() { 03 char prnt =
* ; 04 int i, j, k, s, sp, nos = 0, nosp = -1; 05 for (i = 9; i >= 3; (i = i - 2
)) { 06 for (s = nos; s >= 1; s--) { 07 printf(" ");
08 } 09 for (j = 1; j <= i; j++) { 10printf("%2c", prnt); 11}

12for (sp = nosp; sp >= 1; sp--) { 13 printf(" ");


14 } 15 for (k = 1; k <= i; k++) { 16 if (i == 9 && k == 1) { 17continue; 18} 19
printf("%2c", prnt); 20} 21nos++; 22nosp = nosp + 2; 23printf("\n"); 24} 25nos =
4; 26for (i = 9; i >= 1; (i = i - 2)) { 27 for (s = nos; s >= 1; s--) { 28 prin
tf(" ");
29 } 30 for (j = 1; j <= i; j++) { 31 printf("%2c", prnt);
32 } 33 nos++; 34 printf("\n"); 35 } 36 37 return 0;

38}
Download Code Back to top
9. Write a C program to print the following pattern:
* *** ***** ******* * ** * **
*** *** ******* *** *** ** * ** *
******* ***** *** *
Program: view source print?

01#include <stdio.h> 02/* 03 * nos = Num. of spaces required in the triangle. 04


* i = Counter for the num. of charcters to print in each row 05 * skip= A flag
for checking whether to 06 * 07 * 08 */ 09int triangle(int nos, int i, int skip)
{ 10 char prnt = * ; 11 int s, j; 12 for (s = nos; s >= 1; s--) { 13 printf("
"); 14 } 15 for (j = 1; j <= i; j++) { 16 if (skip != 0) { 17 18 19 if (i == 4 &
& j == 1) { continue; } skip a character in a row.
20 } 21 printf("%2c", prnt); 22 } 23 return 0; 24} 25 26int main() {

27 int i, nos = 4; 28 for (i = 1; i <= 7; (i = i + 2)) { 29 triangle(nos, i, 0);


30 nos--; 31 printf("\n"); 32 } 33 nos = 5; 34 for (i = 1; i <= 4; i++) { 35tri
angle(1, i, 0); //one space needed in each case of the formation 36triangle(nos,
i, 1); //skip printing one star in the last row. 37nos = nos - 2; 38printf("\n"
); 39} 40nos = 1; 41for (i = 3; i >= 1; i--) { 42 triangle(1, i, 0); 43 triangle
(nos, i, 0); 44 nos = nos + 2; 45 printf("\n"); 46 } 47 nos = 1; 48 for (i = 7;
i >= 1; (i = i - 2)) { 49 triangle(nos, i, 0); 50 nos++; 51 printf("\n"); 52 }

53 return 0; 54}
Download Code Back to top
10. Write a C program to print the following pattern:
************************* * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * *
*************************
Program: view source print? 01#include <stdio.h> 02 03/* 04 * nos = Num. of spac
es required in the triangle. 05 * i = Counter for the num. of characters to prin
t in each row

06 * skip= A flag for check whether to 07 *


, int i, int skip) { 12 char prnt = * ; 13
--) { 15 printf(" "); 16 } 17 for (j = 1; j
if (i == 9 && j == 1) { 20 continue; skip a

08 * 09 */ 10 11int triangle(int nos


int s, j; 14 for (s = nos; s >= 1; s
<= i; j++) { 18 if (skip != 0) { 19
character in a row.

21 } 22 } 23if (i == 1 || i == 9) { 24 printf("%2c", prnt); 25} 26else if (j ==


1 || j == i) { 27 printf("%2c", prnt); 28 } else { 29 printf(" "); 30} } 31retur
n 0; }

32int main() { 33int i, nos = 0, nosp = -1, nbsp = -1; 34for (i = 9; i >= 1; (i
= i - 2)) { 35 triangle(nos, i, 0); 36 triangle(nosp, i, 1); 37 triangle(nbsp, i
, 1); 38 printf("\n"); 39 nos++; 40 nosp = nosp + 2; 41 nbsp = nbsp + 2; 42 } 43
nos = 3, nosp = 5, nbsp = 5; 44 for (i = 3; i <= 9; (i = i + 2)) { 45 triangle(
nos, i, 0); 46 triangle(nosp, i, 1); 47 triangle(nbsp, i, 1); 48 printf("\n"); 4
9 nos--; 50 nosp = nosp - 2; 51 nbsp = nbsp - 2; 52 } 53 return 0; 54}

Number pattern programs


# Write a C program to print the following pattern:
1 01 101 0101 10101
Program: view source print? 01 02 03 04 05 06 07 08 09 10 11 12 13 14 int main(v
oid) { int i, j; for (i = 0; i < 4; i++) { for (j = 0; j <= i; j++) { if (((i +
j) % 2) == 0) { // Decides on as to which digit to print. printf("0"); } else {
printf("1"); } printf("\t"); } printf("\n"); #include <stdio.h>

15 16 17
} return 0; }
Download Code
Explanation: This is a right angle triangle composed of 0s and 1s. Back to top
End of Question1 Start of Question2 # Write C program to print the following pat
tern:
0 11 235 8 13 21
Program: view source print? 01 02 03 04 05 06 int main(void) { int i, j, a = 0,
b = 1, temp = 1; for (i = 1; i <= 4; i++) { for (j = 1; j <= i; j++) { #include
<stdio.h>

07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23
if (i == 1 && j == 1) { // Prints the 0 individually first printf("0"); contin
ue; } printf("%d ", temp); // Prints the next digit in the series //Computes the
series temp = a + b; a = b; b = temp; if (i == 4 && j == 3) { // Skips the 4th
character of the base break; } } printf("\n"); } return 0; }
Download Code
Explanation: This prints the Fibonacci series in a right angle triangle formatio
n where the base has only three characters. Back to top
End of Question2 Start of Question3 # Write C program to print the following pat
tern:

1 121 12321 1234321 12321 121 1


Program: view source print? 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 void se
quence(int x); int main() { /* c taken for columns */ int i, x = 0, num = 7; for
(i = 1; i <= num; i++) { if (i <= (num / 2) + 1) { x = i; } else { x = 8 - i; }
sequence(x); puts("\n"); } #include <stdio.h>

16 17 18 19 20 21 22 23 24 25 26 27 28
return 0; }
void sequence(int x) { int j;
for (j = 1; j < x; j++) { printf("%d", j); } for (j = x; j > 0; j--) { printf("%
d", j); } }
Download Code Back to top
End of Question3 Start of Question4 # Write a C program to print the following p
attern:
2 456 6 7 8 9 10 456 2

Program: view source print? 01 02 03 04 int main(void) { int prnt; #include <std
io.h>
05 int i, j, k, r, s, sp, nos = 3, nosp = 2; //nos n nosp controls the spacing f
actor 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 // Prints the upper
triangle for (i = 1; i <= 5; i++) { if ((i % 2) != 0) { for (s = nos; s >= 1; s-) { printf(" "); } for (j = 1; j <= i; j++) { if (i == 5 && j == 5) { //Provide
s the extra space reqd betn 9 n 10 printf(" "); } prnt = i + j; printf("%2d", pr
nt); } } if ((i % 2) != 0) { printf("\n"); nos--; } // as 10 is a 2 digit no.

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
} // Prints the
(k % 2) != 0) {
k; r++) { prnt
"); nosp++; } }

lower triangle skipin its base.. for (k = 3; k >= 1; k--) { if (


for (sp = nosp; sp >= 1; sp--) { printf(" "); } for (r = 1; r <=
= k + r; printf("%2d", prnt); } } if ((k % 2) != 0) { printf("\n
return 0; }

Download Code
Explanation: This is a diamond formation composed of numbers. The numbers are in
the following order next_no=i+j where next_no = The next no to be printed i = i
ndex of the outer for loop j = index of the inner for loop

Back to top
End of Question4 Start of Question5 # Write a C program to print the following p
attern:
1 333 55555
1 333 55555
7777777 7777777 55555 333 1 55555 333 1
Program: view source print? 01 02 03 04 05 06 07 08 09 10 int main(void) { int i
, j, k, s, p, q, sp, r, c = 1, nos = 13; for (i = 1; c <= 4; i++) { if ((i % 2)
!= 0) { // Filters out the even line nos. for (j = 1; j <= i; j++) { // The uppe
r left triangle printf("%2d", i); } for (s = nos; s >= 1; s--) { // The spacing
factor #include <stdio.h>

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
printf(" "); } for (k = 1; k <= i; k++) { // The upper right triangle printf("%2
d", i); } printf("\n"); nos = nos - 4; // Space control ++c; } } nos = 10; // Sp
ace control re intialized c = 1; for (p = 5; (c < 4 && p != 0); p--) { if ((p %
2) != 0) { // Filters out the even row nos for (q = 1; q <= p; q++) { // Lower l
eft triangle printf("%2d", p); } for (sp = nos; sp >= 1; sp--) { // Spacing fact
or printf(" "); } for (r = 1; r <= p; r++) { // Lower right triangle printf("%2d
", p); }
printf("\n"); --c;

37 38 39 40 41 42
nos = nos + 8; // Spacing control. } }
return 0; }
Download Code
Explanation: Here we are printing only the odd row nos along with thier respecti
ve line number. This structure can divided into four identical right angle trian
gles which are kind of twisted and turned placed in a particular format . Back t
o top
End of Question5 Start of Question6 # Write a C program to print the following p
attern:
0 -2-3 0 -4-3-2-1 0 -2-3 0 0
Program: view source print?

01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#include <stdio.h>
int main(void) { int i, j, k, r, s, sp, nos = 2, nosp = 1; for (i = 1; i <= 5; i
++) { if ((i % 2) != 0) { for (s = nos; s >= 1; s--) { //for the spacing factor.
printf(" "); } for (j = 1; j <= i; j++) { printf("%2d", j-i); } } if ((i % 2) !
= 0) { printf("\n"); nos--; } } for (k = 3; k >= 1; k--) { if ((k % 2) != 0) { f
or (sp = nosp; sp >= 1; sp--) { // for the spacing factor. printf(" "); } for (r
= 1; r <= k; r++) { printf("%2d", r-k); }

27 28 29 30 31 32 33 34
} if ((k % 2) != 0) { printf("\n"); nosp++; } } return 0; }
Download Code
Explanation:This can be seen as a diamond composed of numbers. If we use the con
ventional nested for loop for its construction the numbers can be seen to flowin
g the following function f(x) -> j-i where j= inner loop index i= outer loop ind
ex Back to top
End of Question6 Start of Question7 # Write a C program to print the following p
attern:
77777777777 7 7 7 7

7 7 7 7 7 7
Program: view source print? 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 int
main(void) { int i, j; for (i = 11; i >= 1; i--) { for (j = 1; j <= i; j++) { if
(i == 11) { printf("7"); // Makes sure the base is printed completely continue;
} else if (j == i) { // Hollows the rest printf("7"); } else { printf(" "); } }
printf("\n"); #include <stdio.h>

17 18 19
} return 0; }
Download Code
Explanation: This can be seen as a hollow right-angled triangle composed of 7s Ba
ck to top
End of Question7 Start of Question8 # Write a C program to print the following p
attern:
1 10 101 1010 10101
1 10 101 1010 10101
101010 101010 1010101010101 101010 101010 10101 1010 101 10 1 10101 1010 101 10
1

Program: view source print? 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 1


8 19 20 21 22 23 } if ((k%2)!=0) { // Applying the condition printf(" 1"); } els
e { } for (k=1; k<=i; k++) { if(i==7 && k==1) // Skipping the extra 1 { continue
; } for (s=nos; s>=1; s--) { // Space factor printf(" "); } int main(void) { int
i,j,k,s,nos=11; for (i=1; i<=7; i++) { for (j=1; j<=i; j++) { if ((j%2)!=0) { /
/ Applying the condition printf(" 1"); } else { printf(" 0"); #include <stdio.h>

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 }
} } } } } } }
printf(" 0");
printf("\n"); nos=nos-2; // Space Control
nos=1; for ( i=6; i>=1; i--) { // It shares the same base for (j=1; j<=i; j++) {
if (j%2!=0) { printf(" 1"); } else { printf(" 0");
for(s=nos; s>=1; s--) // Spacing factor { printf(" ");
for (k=1; k<=i; k++) { if (k%2!=0) { printf(" 1"); } else { printf(" 0");

50 51 52 53 54 } }
printf("\n"); nos=nos+2;
return 0;
Download Code Back to top
End of Question8 Start of Question9 # Write a C program to print the following p
attern:
1 24 369 24 1
Program: view source print? 01 02 03 04 05 #include <stdio.h> int main(void) { i
nt i,j; for (i=1; i<=3 ; i++) { for (j=1; j<=i; j++) {

06 07 08 09 10 11 12 13 14 15 16 17 } } } } }
printf("%2d", (i*j));
printf("\n");
for (i=2; i>=1; i--) { // As they share the same base for (j=1; j<=i; j++) { pri
ntf("%2d",i*j);
printf("\n");
return 0;
Download Code
Explanation: This can be seen as two right angle triangles sharing th same base
The numbers are following the following function f(x) = i *j where i = Index of
the Outer loop j = Index of the inner loop Back to top
End of Question9 Start of Question10 # Write a C program to print the following
pattern:
1

10 100 1000 10000 100000 1000000 100000 10000 1000 100 10 1


Program: view source print? 01 02 03 04 05 06 07 08 09 10 int main(void) { int i
,j; for (i=1; i<=7; i++) { for (j=1; j<=i; j++) { if (j==1) { printf(" 1"); } el
se { printf(" 0"); // Applying the condition #include <stdio.h>

11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 } } } } }
}
printf("\n");
for (i=6; i>=1; i--) { //As it shares the same base i=6 for (j=1; j<=i; j++) { i
f (j==1) { // Applying the condition printf(" 1"); } else { printf(" 0"); }
printf("\n");
return 0;
Download Code
Explanation: This can be seen as two right angle triangles sharing the same base
which is composed of 0s n 1s. The first column is filled with 1s and rest with 0s

S-ar putea să vă placă și