10 Challenging number pattern programs in C
This is a continuation to the series of challenging pattern C programs in Interview Mantra. This set of 10 puzzling programs are of type number patterns.
Also read Print Pattern Programs
- Write a C program to print the following pattern:
1 0 1 1 0 1 0 1 0 1 1 0 1 0 1
- Write a C program to print the following pattern:
0 1 1 2 3 5 8 13 21
- Write a C program to print the following pattern:
1 121 12321 1234321 12321 121 1
- Write a C program to print the following pattern:
2 4 5 6 6 7 8 9 10 4 5 6 2
- Write a C program to print the following pattern:
1 1 3 3 3 3 3 3 5 5 5 5 5 5 5 5 5 5 7 7 7 7 7 7 7 7 7 7 7 7 7 7 5 5 5 5 5 5 5 5 5 5 3 3 3 3 3 3 1 1
- Write a C program to print the following pattern:
0 -2-3 0 -4-3-2-1 0 -2-3 0 0
- Write a C program to print the following pattern:
77777777777 7 7 7 7 7 7 7 7 7 7
- Write a C program to print the following pattern:
1 1 1 0 1 0 1 0 1 1 0 1 1 0 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 1 0 1 0 1 1
- Write a C program to print the following pattern:
1 2 4 3 6 9 2 4 1
- Write a C program to print the following pattern:
1 1 0 1 0 0 1 0 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0 1 0 0 1 0 1
- Write a C program to print the following pattern:
- Write C program to print the following pattern:
0 1 1 2 3 5 8 13 21
Program: - Write C program to print the following pattern:
1 121 12321 1234321 12321 121 1
Program: - Write a C program to print the following pattern:
2 4 5 6 6 7 8 9 10 4 5 6 2
Program: - Write a C program to print the following pattern:
1 1 3 3 3 3 3 3 5 5 5 5 5 5 5 5 5 5 7 7 7 7 7 7 7 7 7 7 7 7 7 7 5 5 5 5 5 5 5 5 5 5 3 3 3 3 3 3 1 1
Program: - Write a C program to print the following pattern:
0 -2-3 0 -4-3-2-1 0 -2-3 0 0
Program: - Write a C program to print the following pattern:
77777777777 7 7 7 7 7 7 7 7 7 7
Program: - Write a C program to print the following pattern:
1 1 1 0 1 0 1 0 1 1 0 1 1 0 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 1 0 1 0 1 1
Program: - Write a C program to print the following pattern:
1 2 4 3 6 9 2 4 1
Program: - Write a C program to print the following pattern:
1 1 0 1 0 0 1 0 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0 1 0 0 1 0 1
Program:
1 0 1 1 0 1 0 1 0 1 1 0 1 0 1Program:
#include <stdio.h> int main(void) { 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"); } return 0; }Download Code
Explanation: This is a right angle triangle composed of 0′s and 1′s.
Back to top
End of Question1 Start of Question2
#include <stdio.h> int main(void) { int i, j, a = 0, b = 1, temp = 1; for (i = 1; i <= 4; i++) { for (j = 1; j <= i; j++) { if (i == 1 && j == 1) { // Prints the '0' individually first printf("0"); continue; } 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 formation where the base has only three characters.
Back to top
End of Question2 Start of Question3
#include <stdio.h> void sequence(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"); } 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
#include <stdio.h> int main(void) { int prnt; int i, j, k, r, s, sp, nos = 3, nosp = 2; //nos n nosp controls the spacing factor // 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) { //Provides the extra space reqd betn 9 n 10 printf(" "); // as 10 is a 2 digit no. } prnt = i + j; printf("%2d", prnt); } } if ((i % 2) != 0) { printf("\n"); nos--; } } // Prints the lower triangle skipin its base.. for (k = 3; k >= 1; k--) { if ((k % 2) != 0) { for (sp = nosp; sp >= 1; sp--) { printf(" "); } for (r = 1; r <= k; r++) { prnt = k + r; printf("%2d", prnt); } } if ((k % 2) != 0) { printf("\n"); nosp++; } } 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 = index of the outer for loop
j = index of the inner for loop
Back to top
End of Question4 Start of Question5
#include <stdio.h> 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 upper left triangle printf("%2d", i); } for (s = nos; s >= 1; s--) { // The spacing factor printf(" "); } for (k = 1; k <= i; k++) { // The upper right triangle printf("%2d", i); } printf("\n"); nos = nos - 4; // Space control ++c; } } nos = 10; // Space 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 left triangle printf("%2d", p); } for (sp = nos; sp >= 1; sp--) { // Spacing factor printf(" "); } for (r = 1; r <= p; r++) { // Lower right triangle printf("%2d", p); } printf("\n"); --c; nos = nos + 8; // Spacing control. } } return 0; }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
End of Question5 Start of Question6
#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) { for (sp = nosp; sp >= 1; sp--) { // for the spacing factor. printf(" "); } for (r = 1; r <= k; r++) { printf("%2d", r-k); } } 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 conventional nested for loop for its construction the numbers can be seen to flowing the following function f(x) -> j-i
where
j= inner loop index
i= outer loop index
Back to top
End of Question6 Start of Question7
#include <stdio.h> 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"); } return 0; }Download Code
Explanation: This can be seen as a hollow right-angled triangle composed of 7′s
Back to top
End of Question7 Start of Question8
#include <stdio.h> 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"); } } for (s=nos; s>=1; s--) { // Space factor printf(" "); } for (k=1; k<=i; k++) { if(i==7 && k==1) // Skipping the extra 1 { continue; } if ((k%2)!=0) { // Applying the condition printf(" 1"); } else { 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"); } } printf("\n"); nos=nos+2; } return 0; }Download Code
Back to top
End of Question8 Start of Question9
#include <stdio.h> int main(void) { int i,j; for (i=1; i<=3 ; i++) { for (j=1; j<=i; j++) { printf("%2d", (i*j)); } printf("\n"); } for (i=2; i>=1; i--) { // As they share the same base for (j=1; j<=i; j++) { printf("%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
#include <stdio.h> int main(void) { int i,j; for (i=1; i<=7; i++) { for (j=1; j<=i; j++) { if (j==1) { // Applying the condition printf(" 1"); } else { printf(" 0"); } } printf("\n"); } for (i=6; i>=1; i--) { //As it shares the same base i=6 for (j=1; j<=i; j++) { if (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 0′s n 1′s. The first column is filled with 1′s and rest with 0′s
Back to top End of Question10
No comments:
Post a Comment
Vulgar language in this blog is PROHIBITED....