C Interview Programs To Print Number Patterns
C Interview Programs To Print Number Patterns
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:
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
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
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;
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
------
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
--------
no. sum
--------
1 1
2 3
3 6
4 10
5 15
--------
Program:
view source
print?
01 #include<stdio.h>
02 int main() {
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.