Easy Tutorial
❮ C Examples Time Structure C Continue Statement ❯

C Practice Example 1

C Language Classic 100 Examples

Question: Given the numbers 1, 2, 3, 4, how many different three-digit numbers with distinct digits can be formed? What are they?

Program Analysis: The digits for the hundreds, tens, and units places can all be 1, 2, 3, 4. Form all possible permutations and then remove those that do not meet the condition.

Example

#include<stdio.h>

int main()
{
    int i,j,k;
    printf("\n");
    for(i=1;i&lt;5;i++) { // The following is a triple loop
        for(j=1;j&lt;5;j++) {
            for (k=1;k&lt;5;k++) { // Ensure i, j, k are distinct
                if (i!=k&&i!=j&&j!=k) { 
                    printf("%d,%d,%d\n",i,j,k);
                }
            }
        }
    }
}

The above example outputs:

1,2,3
1,2,4
1,3,2
1,3,4
1,4,2
1,4,3
2,1,3
2,1,4
2,3,1
2,3,4
2,4,1
2,4,3
3,1,2
3,1,4
3,2,1
3,2,4
3,4,1
3,4,2
4,1,2
4,1,3
4,2,1
4,2,3
4,3,1
4,3,2

C Language Classic 100 Examples

❮ C Examples Time Structure C Continue Statement ❯