This post has few of basic number pattern programs in C. Question.56(multiplication table) tests the ability to convert an elementary algorithm into a program. Question.57, Question.58 are programs that test the ability of candidate to use nested while/for loops to print special number patterns. Question.59, Question.60 test ability of using simple while/for loops and basics of formatting using printf statement. See all C interview questions in this blog in one page.
Also Read:
Showing 5 out of 65 C Interview Questions.
- Write a program to display the multiplication table of a given number.
- Write C program to print the following pattern:
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 - 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 - Write a C program to display the following format:
------ a b ------ 1 5 2 4 3 3 4 2 5 1 ------
- Write a C program to display the following format:
-------- no. sum -------- 1 1 2 3 3 6 4 10 5 15 --------
- Write a program to display the multiplication table of a given number.
- Write C program to print the following pattern:
- Write C program to print the following pattern:
- Write a C program to display the following format:
- Write a C program to display the following format:
Program: Multiplication table of a given number
#include <stdio.h>
int main() {
int num, i = 1;
printf("\n Enter any Number:");
scanf("%d", &num);
printf("Multiplication table of %d: \n", num);
while (i <= 10) {
printf("\n %d x %d = %d", num, i, num * i);
i++;
}
return 0;
}
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.
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Program:
#include<stdio.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(" ");
}
for (j = 1; j <= i; j++) {
/* %2d ensures that the number is printed in
two spaces for alignment and the numbers are printed in the order. */
printf("%2d", i);
}
printf("\n");
/*c is decremented by 1 */
c--;
}
return 0;
}
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.
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:
#include<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++) {
printf(" ");
}
for (j = 1; j <= i; j++) { printf("%2d", j); } for (m = j - 2; m > 0; m--) {
/* %2d ensures that the number
* is printed in two spaces
* for alignment */
printf("%2d", m);
}
printf("\n");
/* c is decremented by 2 */
c = c - 2;
}
return 0;
}
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.
------ a b ------ 1 5 2 4 3 3 4 2 5 1 ------
Program:
#include<stdio.h>
int main() {
int i = 1, j = 5;
printf("----------\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 incremented
by 1 for every iteration */
i++;
j--;
}
printf("----------");
return 0;
}
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:
#include<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;
printf("%d \t %d\n", num, sum);
/* num incremented by 1
* for every time
* the loop is executed */
num++;
}
printf("-----------");
return 0;
}
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.
| Previous | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | Next |