Q13 Write a program to generate all combinations of 1, 2 and 3 using for loop.

Write a c program to generate all combinations of 1, 2 and 3 using for loop.

Program: 106

Write a c program to generate all combinations of 1, 2 and 3 using for loop.

#include<stdio.h>
#include<conio.h>
int main()
{
    int i, j, k;    //for three numbers 1, 2 and 3

    for(i=1;i<=3;i++){
        for(j=1;j<=3;j++){
            for(k=1;k<=3;k++){

                //First Method
                //printf("%d%d%d\n", i, j, k);

                //Second Method
                //for removing repeated numbers we have to apply a condition (using if)

                if(i!=j && i!=k && j!=k){
                        printf("%d%d%d\n", i, j, k);
                }


            }
        }
    }
}

Output:

123
132
213
231
312
321

Lokesh Kumar: Being EASTER SCIENCE's founder, Lokesh Kumar wants to share his knowledge and ideas. His motive is "We assist you to choose the best", He believes in different thinking.
Related Post
Leave a Comment

This website uses cookies.