The Program in C that generates all combinations of 1, 2, 3 using for loop is given below:
#include <stdio.h>
int main() {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
for (int 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
Pro-Tips💡
This program uses three nested for loops, one for each number (1, 2, and 3).
The outermost loop runs for i = 1, 2, 3. The second loop runs for j = 1, 2, 3, and the innermost loop runs for k = 1, 2, 3. This will print all the possible combinations of 1, 2, and 3
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.