How to Write a Program That Prints All Possible Different Combinations of Three Digits in C
Introduction
We have done this for single digits numbers, double digits numbers, we want to use the same application to achieve the same thing for the three digits number too, using the putchar where no digit is to be repeated (e.g. 000, 111, 222, 333 etc.) and no combination of same digits should be repeated (e.g. 001, 010, 100; 002, 020, 200; 012, 120, 210; 023, 203, 302 etc.).
How to Generate and Print all Possible Combinations of Three Digits in C without Repetition
We are going to be using the same concept we used for the two digits here too, but this time around we will bring in a third variable, which is the k variable to represent the third digit, the first digit will be represented by the i, the second variable by j.
You can check for certain explanations on how we were able to achieve this same concept for the two digits numbers here because it is the same thing we did.
I will be using the Betty style of C coding, so let's get our hands dirty;
#include <stdio.h>
/**
*main - prints possible combination of three digits
*Return: 0
*/
int main(void)
{
int i, j, k;
for (i = '0'; i <= '9'; i++)
{
for (j = i + 1; j <= '9'; j++)
{
for (k = j + 1; k <= '9'; k++)
{
putchar(i);
putchar(j);
putchar(k);
if (i < '7' || j < '8' || k < '9')
{
putchar(',');
putchar(' ');
}
}
}
}
putchar('\n');
return (0);
}
If you observe, you would see that I didn't add the '0' for this one to convert the number into a character that can be printed to the standard output using the putchar function, instead, I used the numbers inside a single quote, that was something I observed while experimenting, so even for the single and double digits I have written about that I added '0' to change the numeric form of the number to a character, you can simply put all the digits you are using inside a single quote, except the 1 we are adding to the initial value of j and k.
We also used the if control statement to stop the comma and space just before the last number that will be printed. We stopped at 789 because that is the last number that doesn't have any form of repetition if we consider it, 789 could have a repetition of 879, 897, 978, and 987 which are all above 789, so 789 is the first of it's kind. If we check the next number, which would have been 790, then the possible repetitions could be 079 and 097, which we all know that the 079 would have been printed first, thus making any other form of repetition of such not to be printed.
Conclusion
This is one of the many ways that one can use to print three-digit numbers that are unique without any form of repetition, you can also make use of other functions that prints to the standard output (eg. printf, putc and puts etc.)
Thank you for reading. I will love to connect with you on Twitter and LinkedIn.
Subscribe to my newsletter
Read articles from Gideon Bature directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Gideon Bature
Gideon Bature
A Seasoned Software Engineer and Technical Writer.