Вы находитесь на странице: 1из 6

C Interview Programs to print number patterns

1. Write a program to display the multiplication table of a given number.

Program: Multiplication table of a given number

view source
print?
01 #include <stdio.h>
02 int main() {

03 int num, i = 1;
04 printf("\n Enter any Number:");
05 scanf("%d", &num);
06 printf("Multiplication table of %d: \n", num);
07 while (i <= 10) {
08 printf("\n %d x %d = %d", num, i, num * i);
09 i++;
10 }
11 return 0;
12 }

Download Code

Output:

Enter any Number:5


5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

Explanation: We need to multiply the given number (i.e. the number for which we want
the multiplication table) with value of ‘i’ which increments from 1 to 10.

Back to top

2. Write C program to print the following pattern:


1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

Program:

view source
print?
01 #include<stdio.h>
02 int main() {

03 int i, j, k, c = 5;
04 for (i = 1; i <= 5; i++) {
05 /* k is taken for spaces */
06 for (k = 1; k <= c; k++) {
07 /* blank space */
08 printf(" ");

09 }
10 for (j = 1; j <= i; j++) {
11 /* %2d ensures that the number is printed in
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:

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
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

3. Write C program to print the following pattern:

1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1

Program:

view source
print?
01 #include<stdio.h>
02 int main() {
03 /* c taken for columns */
04 int i, j, c = 9, m, k;

05 for (i = 1; i <= 5; i++) {


06 /* k is used for spaces */
07 for (k = 1; k <= c; k++) {
08 printf(" ");

09 }
for (j = 1; j <= i; j++) { printf("%2d",
10
j); } for (m = j - 2; m > 0; m--) {
11 /* %2d ensures that the number
12 * is printed in two spaces
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:

1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1

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

4. Write a C program to display the following format:

------
a b
------
1 5
2 4
3 3
4 2
5 1
------

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");
07 /* logic: while loop repeats
08 * 5 times i.e. until
09 * the condition i<=5 fails */
10 while (i <= 5) {
11 /* i and j value printed */
12 printf("%d \t %d\n", i, j);
13 /* i and j value incremented
14 by 1 for every iteration */
15 i++;
16 j--;
17 }
18 printf("----------");
19 return 0;
20 }

Download Code

Output:

------
a b
------
1 5
2 4
3 3
4 2
5 1
------

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

5. 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() {

03 int num = 1, sum = 0;


04 printf("-----------\n");
05 printf("num \t sum\n");
06 printf("-----------\n");
07 /* while loop repeats 5 times
08 * i.e. until the condition
09 * num <= 5 fails */
10 while (num <= 5) {

11 sum = sum + num;


12 printf("%d \t %d\n", num, sum);
13 /* num incremented by 1
14 * for every time
15 * the loop is executed */
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.