How to Write a Program That Prints All Possible Combinations of Two Two-Digit Numbers in C

How to Write a Program That Prints All Possible Combinations of Two Two-Digit Numbers in C

A Step-by-Step Guide

Introduction

In this guide you will learn how to print all possible combinations of Two Two-digit numbers in C, each two-digits are separated with a space between them, and the two digits have a comma followed by a space before the next pair of two-digits number.

We will still be using our putchar and also there won't be any repetition of any sort between all the digits.

How to Generate and Print all Possible Combinations of Two Two-Digit Numbers in C

We will be making use of the same method we used for printing all possible two-digit numbers here too, so you can check it here if you want to refresh your understanding of that.

But in this case, we will be considered from 00 to 99 for each of the numbers, for use to have two two-digits.

Thus, we will be faced with the challenge of printing two digits to the standard out at once using the putchar function as the function is only able to print just a single digit.

We will solve this problem by printing the same number twice, but to use the putchar to display them at once, we will take advantage of the first digit as well as the last digit of the number. Follow me as I explain how we are going to pull it off;

So since the numbers we will be printing at a time are two digits, to get the first digit of a two-digit number, we can do that by simply dividing the number by 10 i.e. number/10. While to get the last digit of that same number, we can simply divide the number using the modulus of 10. i.e. number%10. And don't forget how we are going to add '0' to each of the digits to convert them from numbers to characters so that they can be printed to the standard out using the putchar function as this function only accepts characters and not direct numbers.

Now we can proceed, so using the same concept as the two-digits combination, we will be having our code as;

#include <stdio.h>
/**
  *main - prints two pairs of two-digit numbers
  *Return: 0
*/
int main(void)
{
    int i, j;

    for (i = 0; i <= 99; i++)
        {
            for (j = i + 1; j <= 99; j++)
            {
                putchar((i / 10) + '0');
                putchar((i % 10) + '0');
                putchar(' ');
                putchar((j / 10) + '0');
                putchar((j % 10) + '0');

                if (i < 98 || j < 99)
                {
                    putchar(',');
                    putchar(' ');
                }
            }
        }
            putchar('\n');
            return (0);
}

I know you might be wondering why didn't we use the trick of quoting the numbers with a single quote to change them to a character, if it were to be a single digit, it would have worked, but for a double-digit, it won't work.

Compile the run the code, and you will get something like this:

You should get something like what is in the images, I showed the first and last part of the numbers that were generated.

Conclusion

This is how you can print out pairs of two-digit numbers that are separated with a space in between them.

Thank you for reading, you can connect with me on Twitter and LinkedIn.