Mathematically-speaking there is only one combination of a 3-digit number. When dealing with a combination of digits, the order of those digits does not matter, thus 123 and 321 are the exact same combination. If the order of the digits is important then it is a permutation, not a combination. That is, 123 and 321 are completely different permutations of the same combination of digits.
The confusion is understandable given that we commonly refer to a combination lock instead of a permutation lock. Mathematically speaking, a combination lock that unlocks with the code 123 would also unlock with the codes 132, 213, 231, 312 and 321, because all six permutations of the digits 1, 2 and 3 are in fact the same combination. Some combination locks really do work this way, however the vast majority are actually permutation locks; we call them combination locks simply because we don't normally use the term "combination" in the much stricter mathematical sense.
To restate the question: How do you write a C program to print all permutations of a 3-digit number?
A 3-digit number has 6 permutations, thus we can print all six by treating the number as an array, and printing all 6 permutations of the array:
void print_permutations (int num) { char set[3];
int index;
if (num<100 num > 999) return; // not a 3-digit number
index = 0;
while (num>0) {
set[index] = num % 10; // least-significant digit
num /= 10; // shift all digits one position to the right
}
// sort the array in ascending order
if (set[0]>set[1]) set[0]^=set[1]^=set[0]^=set[1];
if (set[1]>set[2]) set[1]^=set[2]^=set[1]^=set[2];
if (set[0]>set[1]) set[0]^=set[1]^=set[0]^=set[1];
// print the permutations
printf ("%d%d%d\n", set[0], set[1], set[2]); printf ("%d%d%d\n", set[0], set[2], set[1]); printf ("%d%d%d\n", set[1], set[0], set[2]);
printf ("%d%d%d\n", set[1], set[2], set[0]);
printf ("%d%d%d\n", set[2], set[0], set[1]);
printf ("%d%d%d\n", set[2], set[1], set[0]);
}
The problem with this is when the 3-digit number contains duplicate digits. This would treat 100 as if it had 6 permutations when it really only has 3 {100, 010 and 001}, while 111 only has one permutation {111}. These must be treated as being special cases:
void print_permutations (int num) {
char set[3];
int index;
if (num<100 num > 999) return; // not a 3-digit number
index = 0;
while (num>0) {
set[index] = num % 10; // least-significant digit
num /= 10; // shift all digits one position to the right
}
// sort the array in ascending order
if (set[0]>set[1]) set[0]^=set[1]^=set[0]^=set[1]; // move larger of 1st and 2nd digit to middle
if (set[1]>set[2]) set[1]^=set[2]^=set[1]^=set[2]; // move larger of 2nd and 3rd digit to end
if (set[0]>set[1]) set[0]^=set[1]^=set[0]^=set[1]; // move larger of 1st and 2nd digit to middle
// print the permutations (handle special cases where digits are duplicated)
if (set[0]==set[1] && set[0]==set[2]]) { // same three digits (one permutation)
printf ("%d%d%d\n", set[0], set[1], set[2]);
} else if (set[0]==set[1]) { // first two digits are the same (three permutations)
printf ("%d%d%d\n", set[0], set[1], set[2]); // same as 1, 0, 2
printf ("%d%d%d\n", set[0], set[2], set[1]); // same as 1, 2, 0
printf ("%d%d%d\n", set[2], set[0], set[1]); // same as 2, 1, 0
} else if (set[1]==set[2]) { // last two digits are the same (three permutations)
printf ("%d%d%d\n", set[0], set[1], set[2]); // same as 0, 2, 1
printf ("%d%d%d\n", set[1], set[0], set[2]); // same as 2, 0, 1
printf ("%d%d%d\n", set[2], set[1], set[0]); // same as 1, 2, 0
} else { // all three digits are unique (6 permutations)
printf ("%d%d%d\n", set[0], set[1], set[2]);
printf ("%d%d%d\n", set[0], set[2], set[1]);
printf ("%d%d%d\n", set[1], set[0], set[2]);
printf ("%d%d%d\n", set[1], set[2], set[0]);
printf ("%d%d%d\n", set[2], set[0], set[1]);
printf ("%d%d%d\n", set[2], set[1], set[0]);
}
}
it is int n=123; printf ("%d", (n/10)%10);
wap to print all the arnstrong no. between 100&1000
Q.1 Write a program to print first ten odd natural numbers. Q.2 Write a program to input a number. Print their table. Q.3 Write a function to print a factorial value.
i want to write a simple without using pointer or array c program which will print greatest number when i give 20 number .........How far have you gotten so far?
The print command is a way to display output to the console. The hello world program, for example, can be written in python as simply print("Hello world") Other values can also be used in a print statement: a = 4 print(a) #will print the number 4
it is int n=123; printf ("%d", (n/10)%10);
bghjg
Prime numbers are numbers that are only divisible by themselves and the number 1. You can write a program to print all prime numbers from 1 to 100 in FoxPro.
wap to print all the arnstrong no. between 100&1000
Q.1 Write a program to print first ten odd natural numbers. Q.2 Write a program to input a number. Print their table. Q.3 Write a function to print a factorial value.
There is no such thing as 'the greatest number'. Here is a program in unix to prove it: echo 'for (i=1; i>=0; i= i*2) print i,"\n";' | bc if you want to actually see the output use it this way: echo 'for (i=1; i>=0; i= i*2) print i,"\n";' | bc | less -S
i want to write a simple without using pointer or array c program which will print greatest number when i give 20 number .........How far have you gotten so far?
The print command is a way to display output to the console. The hello world program, for example, can be written in python as simply print("Hello world") Other values can also be used in a print statement: a = 4 print(a) #will print the number 4
nine seven two
write a program that reads a phrase and prints the number of lowercase latters in it using a function for counting? in C program
hhh
find even number in array