Lets write a C program to generate all combinations of 1, 2 and 3 using for loop.
Related Read:
For Loop In C Programming Language
Nested For Loop In C Programming Language
Page Contents
In this program we take 3 for loops. Nesting of for loops go 3 levels deep. For every iteration of outer most for loop, the inner for loop executes 3 times. Similarly, for every iteration of the inner for loop, the inner most for loop executes 3 times. At the end of 3 iteration of outer most for loop, all the combinations of numbers 1, 2 and 3 are generated.
#include<stdio.h> int main() { int i, j, k; for(i = 1; i <= 3; i++) { for(j = 1; j <= 3; j++) { for(k = 1; k <= 3; k++) { if( i != j && i != k && j != k) printf("%d %d %d\n", i, j, k); } } } return 0; }
Output:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
Using a if condition inside inner most for loop, we make sure i, j and k value are not same – hence eliminating the chances of displaying repeated numbers.
#include<stdio.h> int main() { int i, j, k; for(i = 1; i <= 3; i++) { for(j = 1; j <= 3; j++) { for(k = 1; k <= 3; k++) { printf("%d %d %d\n", i, j, k); } } } return 0; }
Output:
1 1 1
1 1 2
1 1 3
1 2 1
1 2 2
1 2 3
1 3 1
1 3 2
1 3 3
2 1 1
2 1 2
2 1 3
2 2 1
2 2 2
2 2 3
2 3 1
2 3 2
2 3 3
3 1 1
3 1 2
3 1 3
3 2 1
3 2 2
3 2 3
3 3 1
3 3 2
3 3 3
#include<stdio.h> int main() { int i, j, k, l; for(i = 1; i <= 4; i++) { for(j = 1; j <= 4; j++) { for(k = 1; k <= 4; k++) { for(l = 1; l <= 4; l++) { printf("%d %d %d %d\n", i, j, k, l); } } } } return 0; }
Output:
1 1 1 1
1 1 1 2
1 1 1 3
1 1 1 4
1 1 2 1
1 1 2 2
1 1 2 3
1 1 2 4
1 1 3 1
1 1 3 2
1 1 3 3
1 1 3 4
1 1 4 1
1 1 4 2
1 1 4 3
1 1 4 4
1 2 1 1
1 2 1 2
1 2 1 3
1 2 1 4
1 2 2 1
1 2 2 2
1 2 2 3
1 2 2 4
1 2 3 1
1 2 3 2
1 2 3 3
1 2 3 4
1 2 4 1
1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 2 4 2
3 2 4 3
3 2 4 4
3 3 1 1
3 3 1 2
3 3 1 3
3 3 1 4
3 3 2 1
3 3 2 2
3 3 2 3
3 3 2 4
3 3 3 1
3 3 3 2
3 3 3 3
3 3 3 4
3 3 4 1
3 3 4 2
3 3 4 3
3 3 4 4
3 4 1 1
3 4 1 2
3 4 1 3
3 4 1 4
3 4 2 1
3 4 2 2
3 4 2 3
3 4 2 4
3 4 3 1
3 4 3 2
3 4 3 3
3 4 3 4
3 4 4 1
3 4 4 2
3 4 4 3
3 4 4 4
4 1 1 1
4 1 1 2
4 1 1 3
4 1 1 4
4 1 2 1
4 1 2 2
4 1 2 3
4 1 2 4
4 1 3 1
4 1 3 2
4 1 3 3
4 1 3 4
4 1 4 1
4 1 4 2
4 1 4 3
4 1 4 4
4 2 1 1
4 2 1 2
4 2 1 3
4 2 1 4
4 2 2 1
4 2 2 2
4 2 2 3
4 2 2 4
4 2 3 1
4 2 3 2
4 2 3 3
4 2 3 4
4 2 4 1
4 2 4 2
4 2 4 3
4 2 4 4
4 3 1 1
4 3 1 2
4 3 1 3
4 3 1 4
4 3 2 1
4 3 2 2
4 3 2 3
4 3 2 4
4 3 3 1
4 3 3 2
4 3 3 3
4 3 3 4
4 3 4 1
4 3 4 2
4 3 4 3
4 3 4 4
4 4 1 1
4 4 1 2
4 4 1 3
4 4 1 4
4 4 2 1
4 4 2 2
4 4 2 3
4 4 2 4
4 4 3 1
4 4 3 2
4 4 3 3
4 4 3 4
4 4 4 1
4 4 4 2
4 4 4 3
4 4 4 4
We can use below condition inside inner most for loop to eliminate displaying repeated numbers. As shown in below code:
#include<stdio.h> int main() { int i, j, k, l; for(i = 1; i <= 4; i++) { for(j = 1; j <= 4; j++) { for(k = 1; k <= 4; k++) { for(l = 1; l <= 4; l++) { if(i != j && i != k && i != l && j != k && j != l && k != l) printf("%d %d %d %d\n", i, j, k, l); } } } } return 0; }
Output:
1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 2 3
1 4 3 2
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 1 3
2 4 3 1
3 1 2 4
3 1 4 2
3 2 1 4
3 2 4 1
3 4 1 2
3 4 2 1
4 1 2 3
4 1 3 2
4 2 1 3
4 2 3 1
4 3 1 2
4 3 2 1
For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List
For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert