answersLogoWhite

0


Best Answer

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]);

}

}

User Avatar

Wiki User

6y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a C program to print all combinations of a 3-digit number?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you print the middle number of a 3digit number using c?

it is int n=123; printf ("%d", (n/10)%10);


C plus plus program to print number patterns?

bghjg


Program for print prime all number from 1 to 100 in foxpro?

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.


How do you solve this program wap to print sum of a digit of an inputed number?

wap to print all the arnstrong no. between 100&amp;1000


Write a java script program to print first ten odd natural numbers in C?

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.


Program to print the greatest number in unix?

There is no such thing as 'the greatest number'. Here is a program in unix to prove it: echo 'for (i=1; i&gt;=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&gt;=0; i= i*2) print i,"\n";' | bc | less -S


You want to write a simple without using pointer or array c program which will print greatest number when you give 20 number?

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?


What does the print command do in Python?

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


Writa a program to print a number in word eg if 972 is entered through the keyboard that should print nine seven two?

nine seven two


Write a program that read phrase and print the number of lower-case letter in it using function of counting?

write a program that reads a phrase and prints the number of lowercase latters in it using a function for counting? in C program


C program to print mark sheet of 5 subjects and the number of students is user input?

hhh


Program to print all the even numbers of an array in visual basic?

find even number in array