Number Problems in C
Number Problems in C
22
333
4444
55555
Program:
view source
print?
01 #include<stdio.h>
02 int main() {
03 int i, j, k, c = 5;
07 /* blank space */
08 printf(" ");
09 }
12 two spaces for alignment and the numbers are printed in the order. */
13 printf("%2d", i);
14 }
15 printf("\n");
16 /*c is decremented by 1 */
17 c--;
18 }
19 return 0;
20 }
Download Code
Output:
22
333
4444
55555
Explanation: Here ‘i’ loop is used for printing the numbers in the respective rows and ‘k’ loop is used for
providing spaces. ‘j’ loop prints the numbers. ‘c’ is decremented for numbers to be displayed in alternate
columns.
Back to top
121
12321
1234321
123454321
Program:
view source
print?
01 #include<stdio.h>
02 int main() {
04 int i, j, c = 9, m, k;
08 printf(" ");
09 }
13 * for alignment */
14 printf("%2d", m);
15 }
16 printf("\n");
17 /* c is decremented by 2 */
18 c = c - 2;
19 }
20 return 0;
21 }
Download Code
Output:
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 is used for printing numbers in reverse
order.
Back to top
------
ab
------
15
24
33
42
51
------
Program:
view source
print?
01 #include<stdio.h>
02 int main() {
03 int i = 1, j = 5;
04 printf("----------\n");
05 printf("a \t b \n");
06 printf("----------\n");
10 while (i <= 5) {
15 i++;
16 j--;
17 }
18 printf("----------");
19 return 0;
20 }
Download Code
Output:
------
ab
------
15
24
33
42
51
------
Explanation: Here, ‘i’ is initialized to least value 1 and ‘j’ initialized to highest value 5. We keep
incrementing the i’ value and decrementing the ‘j’ value until the condition fails. The value is displayed at
each increment and at each decrement. Back 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 #include<stdio.h>
02 int main() {
04 printf("-----------\n");
05 printf("num \t sum\n");
06 printf("-----------\n");
13 /* num incremented by 1
16 num++;
17 }
18 printf("-----------");
19 return 0;
20 }
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 used 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 zero. sum is added to the numbers which are
incremented by ‘i’ and displayed.
* *
* * *
* * * *
* *
*** ***
***** *****
***********
* *
* *
* * * *
* * * *
* * * * *
* * * *
* * * *
* *
* *
* * * * *
* * * *
* * *
* *
* *
* * *
* * * *
* * * * *
***
*****
*******
*********
*******
*****
***
***
*****
**
***
****
***
**
*********
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
*********
*****************
******* *******
***** *****
*** ***
*********
*******
*****
***
***
*****
*******
* *
** **
*** ***
*******
*** ***
** **
* *
*******
*****
***
*************************
* * * * * *
* * * * * *
* * * * * *
* * *
* * * * * *
* * * * * *
* * * * * *
*************************
*
* *
* * *
* * * *
Program:
view source
print?
02
03 #include <stdio.h>
04 int main() {
06 int i, j, nos = 4, s;
09 printf(" ");
10 }
12 printf("%2c", prnt);
13 }
14 printf("\n");
16 }
17 return 0;
18 }
Download Code
Back to top
* *
*** ***
***** *****
***********
Program:
view source
print?
01 #include<stdio.h>
02 int main() {
04 int i, j, k, s, c = 1, nos = 9;
07 if ((i % 2) != 0) {
09 printf("%2c", prnt);
10 }
11 for (s = nos; s >= 1; s--) { //The spacing factor
12 if (c == 4 && s == 1) {
13 break;
14 }
15 printf(" ");
16 }
18 if (c == 4 && k == 5) {
19 break;
20 }
21 printf("%2c", prnt);
22 }
23 printf("\n");
25 ++c;
26 }
27 }
28 return 0;
29 }
Download Code
Back to top
* *
* * * *
* * * *
* * * * *
* * * *
* * * *
* *
* *
Program:
view source
print?
01 #include<stdio.h>
02 int main() {
04 int i, j, k, s, p, r, nos = 7;
05
08 if ((i % 2) != 0 && (j % 2) != 0) {
09 printf("%3c", prnt);
10 }
13 }
14 else {
15 printf(" ");
16 }
17 }
19 printf(" ");
20 }
22 if (i == 5 && k == 1) {
23 continue;
24 }
25 if ((k % 2) != 0) {
26 printf("%3c", prnt);
27 }
28 else {
29 printf(" ");
30 }
31 }
32 printf("\n");
38 printf("%3c", prnt);
39 }
41 printf("%3c", prnt);
42 }
43 else {
44 printf(" ");
45 }
46 }
48 printf(" ");
49 }
51 if ((k % 2) != 0) {
52 printf("%3c", prnt);
53 }
54 else {
55 printf(" ");
56 }
57 }
59 printf("\n");
60 }
61 return 0;
62 }
Download Code
Explanation:
This can be seen as an inverted diamond composed of stars. It can be noted that the composition of
this figure follows sequential pattern of consecutive 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
Back to top
* * * * *
* * * *
* * *
* *
* *
* * *
* * * *
* * * * *
Program:
view source
print?
01 #include<stdio.h>
02 int main() {
04 int i, j, s, nos = 0;
07 printf(" ");
08 }
10 if ((i % 2) != 0 && (j % 2) != 0) {
11 printf("%2c", prnt);
12 } else {
13 printf(" ");
14 }
15 }
16 printf("\n");
17 nos++;
18 }
19 nos = 3;
22 printf(" ");
23 }
25
26 if ((i % 2) != 0 && (j % 2) != 0) {
27 printf("%2c", prnt);
28 } else {
29 printf(" ");
30 }
31 }
32 nos--;
33 printf("\n");
34 }
35 return 0;
36 }
Download Code
Back to top
***
*****
*******
*********
*******
*****
***
***
*****
Program:
view source
print?
01 #include<stdio.h>
02 int main() {
04 int i, j, k, s, nos = 4;
07 printf(" ");
08 }
10 printf("%2c", prnt);
11 }
13 if (i == 1) { continue;
14 }
15 printf("%2c", prnt);
16 }
17 printf("\n"); nos--;
18 }
19 nos = 1;
22 printf(" ");
23 }
25 printf("%2c", prnt);
26 }
28 printf("%2c", prnt);
29 }
30 nos++;
31 printf("\n");
32 }
33 nos = 3;
35 if ((i % 2) != 0) {
37 printf(" ");
38 }
39 for (j = 1; j <= i; j++) {
40 printf("%2c", prnt);
41 }
42 }
43 if ((i % 2) != 0) {
44 printf("\n");
45 nos--;
46 }
47 }
48 return 0;
49 }
Download Code
Back to top
**
***
****
***
**
*
Program:
view source
print?
01 /*
02 This can be seen as two right angle triangles sharing the same base
04 */
05 #include <stdio.h>
07 // factor guided by the outer loop index and the spacing index.
10 int s, j;
12 printf(" ");
13 }
15 printf("%2c", prnt);
16 }
17 return 0;
18 }
19
20 int main() {
21 int i, nos = 5;
26 printf("\n"); }
29 int j = 1;
32 printf("\n");
33 }
34 return 0;
35 }
Download Code
Back to top
*********
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
*********
Program:
view source
print?
01 #include <stdio.h>
02
03 int main() {
08 printf("%2c", prnt);
09 }
11 printf(" ");
12 }
14 if (i == 5 && k == 5) {
15 continue;
16 }
17 printf("%2c", prnt);
18 }
19 nos = nos + 2;
20 printf("\n");
21 }
22 nos = 5;
25 printf("%2c", prnt);
26 }
28 printf(" ");
29 }
31 if (i == 5 && k == 5) {
32 break;
33 }
34 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>
02 int main() {
07 printf(" ");
08 }
11 }
13 printf(" ");
14 }
16 if (i == 9 && k == 1) {
17 continue;
18 }
19 printf("%2c", prnt);
20 }
21 nos++;
22 nosp = nosp + 2;
23 printf("\n");
24 }
25 nos = 4;
28 printf(" ");
29 }
31 printf("%2c", prnt);
32 }
33 nos++;
34 printf("\n");
35 }
36
37 return 0;
38 }
Download Code
Back to top
***
*****
*******
* *
** **
*** ***
*******
*** ***
** **
* *
*******
*****
***
*
Program:
view source
print?
01 #include <stdio.h>
02 /*
07 *
08 */
11 int s, j;
13 printf(" ");
14 }
16 if (skip != 0) {
17 if (i == 4 && j == 1) {
18 continue;
19 }
20 }
21 printf("%2c", prnt);
22 }
23 return 0;
24 }
25
26 int main() {
27 int i, nos = 4;
29 triangle(nos, i, 0);
30 nos--;
31 printf("\n");
32 }
33 nos = 5;
37 nos = nos - 2;
38 printf("\n");
39 }
40 nos = 1;
42 triangle(1, i, 0);
43 triangle(nos, i, 0);
44 nos = nos + 2;
45 printf("\n");
46 }
47 nos = 1;
49 triangle(nos, i, 0);
50 nos++;
51 printf("\n");
52 }
53 return 0;
54 }
Download Code
Back to top
*************************
* * * * * *
* * * * * *
* * * * * *
* * *
* * * * * *
* * * * * *
* * * * * *
*************************
Program:
view source
print?
01 #include <stdio.h>
02
03 /*
08 *
09 */
10
13 int s, j;
15 printf(" ");
16 }
18 if (skip != 0) {
19 if (i == 9 && j == 1) {
20 continue;
21 }
22 }
23 if (i == 1 || i == 9) {
24 printf("%2c", prnt);
25 }
26 else if (j == 1 || j == i) {
27 printf("%2c", prnt);
28 } else {
29 printf(" ");
30 } }
31 return 0; }
32 int main() {
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 }
45 triangle(nos, i, 0);
46 triangle(nosp, i, 1);
47 triangle(nbsp, i, 1);
48 printf("\n");
49 nos--;
50 nosp = nosp - 2;
51 nbsp = nbsp - 2;
52 }
53 return 0;
54 }
01
101
0101
10101
Program:
view source
print?
01 #include <stdio.h>
02
03 int main(void) {
04 int i, j;
05 for (i = 0; i < 4; i++) {
08 printf("0");
09 } else {
10 printf("1");
11 }
12 printf("\t");
13 }
14 printf("\n");
15 }
16 return 0;
17 }
Download Code
Back to top
11
235
8 13 21
Program:
view source
print?
01 #include <stdio.h>
02
03 int main(void) {
04 int i, j, a = 0, b = 1, temp = 1;
08 printf("0");
09 continue;
10 }
13 temp = a + b;
14 a = b;
15 b = temp;
17 break;
18 }
19 }
20 printf("\n");
21 }
22 return 0;
23 }
Download Code
Explanation: This prints the Fibonacci series in a right angle triangle formation where the base has only
three characters.
Back to top
121
12321
1234321
12321
121
Program:
view source
print?
01 #include <stdio.h>
02
03 void sequence(int x);
04 int main() {
06 int i, x = 0, num = 7;
08 if (i <= (num / 2) + 1) {
09 x = i;
10 } else {
11 x = 8 - i;
12 }
13 sequence(x);
14 puts("\n");
15 }
16 return 0;
17 }
18
19 void sequence(int x) {
20 int j;
21
23 printf("%d", j);
24 }
26 printf("%d", j);
27 }
28 }
Download Code
Back to top
456
6 7 8 9 10
456
Program:
view source
print?
01 #include <stdio.h>
02
03 int main(void) {
04 int prnt;
05 int i, j, k, r, s, sp, nos = 3, nosp = 2; //nos n nosp controls the spacing factor
08 if ((i % 2) != 0) {
09 for (s = nos; s >= 1; s--) {
10 printf(" ");
11 }
15 }
16 prnt = i + j;
17 printf("%2d", prnt);
18 }
19 }
20 if ((i % 2) != 0) {
21 printf("\n");
22 nos--;
23 }
24 }
27 if ((k % 2) != 0) {
29 printf(" ");
30 }
32 prnt = k + r;
33 printf("%2d", prnt);
34 }
35 }
36 if ((k % 2) != 0) {
37 printf("\n");
38 nosp++;
39 }
40 }
41 return 0;
42 }
Download Code
Explanation: This is a diamond formation composed of numbers. The numbers are in the following order
next_no=i+j where
Back to top
1 1
333 333
55555 55555
7777777 7777777
55555 55555
333 333
1 1
Program:
view source
print?
01 #include <stdio.h>
02
03 int main(void) {
08 printf("%2d", i);
09 }
11 printf(" ");
12 }
14 printf("%2d", i);
15 }
16 printf("\n");
18 ++c;
19 }
20 }
22 c = 1;
26 printf("%2d", p);
27 }
29 printf(" ");
30 }
32 printf("%2d", p);
33 }
34
35 printf("\n");
36 --c;
38 }
39 }
40
41 return 0;
42 }
Download Code
Explanation: Here we are printing only the odd row nos along with thier respective line number. This
structure can divided into four identical right angle triangles which are kind of twisted and turned placed
in a particular format .
Back to top
-2-3 0
-4-3-2-1 0
-2-3 0
Program:
view source
print?
01 #include <stdio.h>
02
03 int main(void) {
06 if ((i % 2) != 0) {
09 }
11 printf("%2d", j-i);
12 }
13 }
14 if ((i % 2) != 0) {
15 printf("\n");
16 nos--;
17 }
18 }
20 if ((k % 2) != 0) {
22 printf(" ");
23 }
25 printf("%2d", r-k);
26 }
27 }
28 if ((k % 2) != 0) {
29 printf("\n");
30 nosp++;
31 }
32 }
33 return 0;
34 }
Download Code
Explanation:This can be seen as a diamond composed of numbers. If we use the conventional nested for
loop for its construction the numbers can be seen to flowing the following function f(x) -> j-i
where
Back to top
77777777777
7
Program:
view source
print?
01 #include <stdio.h>
02
03 int main(void) {
04 int i, j;
07 if (i == 11) {
09 continue;
11 printf("7");
12 } else {
13 printf(" ");
14 }
15 }
16 printf("\n");
17 }
18 return 0;
19 }
Download Code
Explanation: This can be seen as a hollow right-angled triangle composed of 7′s
Back to top
1 1
10 10
101 101
1010 1010
10101 10101
101010 101010
1010101010101
101010 101010
10101 10101
1010 1010
101 101
10 10
1 1
Program:
view source
print?
01 #include <stdio.h>
02
03 int main(void) {
04 int i,j,k,s,nos=11;
08 printf(" 1");
09 } else {
10 printf(" 0");
11 }
12 }
14 printf(" ");
15 }
18 {
19 continue;
20 }
22 printf(" 1");
23 } else {
24 printf(" 0");
25 }
26 }
27 printf("\n");
29 }
30 nos=1;
33 if (j%2!=0) {
34 printf(" 1");
35 } else {
36 printf(" 0");
37 }
38 }
40 {
41 printf(" ");
42 }
44 if (k%2!=0) {
45 printf(" 1");
46 } else {
47 printf(" 0");
48 }
49 }
50 printf("\n");
51 nos=nos+2;
52 }
53 return 0;
54 }
Download Code
Back to top
24
369
24
Program:
view source
print?
01 #include <stdio.h>
02 int main(void) {
03 int i,j;
06 printf("%2d", (i*j));
07 }
08 printf("\n");
09 }
12 printf("%2d",i*j);
13 }
14 printf("\n");
15 }
16 return 0;
17 }
Download Code
Explanation: This can be seen as two right angle triangles sharing th same base
where
Back to top
1
10
100
1000
10000
100000
1000000
100000
10000
1000
100
10
Program:
view source
print?
01 #include <stdio.h>
02
03 int main(void) {
04 int i,j;
08 printf(" 1");
09 } else {
10 printf(" 0");
11 }
12 }
13 printf("\n");
14 }
15 for (i=6; i>=1; i--) { //As it shares the same base i=6
18 printf(" 1");
19 } else {
20 printf(" 0");
21 }
22 }
23 printf("\n");
24 }
25 return 0;
26 }
Download Code
Explanation: This can be seen as two right angle triangles sharing the same base which is composed of
0′s n 1′s. The first column is filled with 1′s and rest with 0′s