# C Program to print Triad Numbers

Last updated on September 23, 2020

## What are triad numbers? #

The triad numbers is a set of three numbers which satisfy the following condition:

- Each number must be a 3 digit number
- All the digits in the numbers must be unique.
- The second number must be twice the first number and the third number must be thrice the first.

Here are examples of tried numbers:

1 2 3 4 5 | ```
192 384 576
219 438 657
267 534 801
273 546 819
327 654 981
``` |

The following is a C program to print all triad numbers between 100 and 999.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | ```
/*******************************************
* C Program to print all the triad numbers
********************************************/
#include<stdio.h> // include stdio.h
int main()
{
int d1, d2, d3, tmp;
// generate three digit numbers
for (int num = 100; num <= 333; num++)
{
// check for unique digits in the first number
for (int i = num; i <= num * 3; i += num)
{
tmp = i;
d1 = tmp % 10;
tmp = tmp / 10;
d2 = tmp % 10;
tmp = tmp / 10;
d3 = tmp % 10;
if (d1 == d2 || d2 == d3 || d3 == d1) {
goto next;
}
}
// check whether the given three numbers have unique digits or not
for (int a = num; a > 0; a /= 10)
{
d1 = a % 10;
for (int b = num * 2; b > 0; b /= 10)
{
d2 = b % 10;
for (int c = num * 3; c > 0; c /= 10)
{
d3 = c % 10;
if (d1 == d2 || d2 == d3 || d3 == d1)
{
goto next;
}
}
}
}
printf("%d %d %d\n", num, num * 2, num * 3);
next:;
}
return 0;
}
``` |

**Expected Output:**

1 2 3 4 5 | ```
192 384 576
219 438 657
267 534 801
273 546 819
327 654 981
``` |

## How it works #

Here are a few things to notice about this program:

- The first loop is used to generate three-digit numbers. Notice that we are running the loop from 100 to 333, this is because any number greater than 333, will result in a number greater than 999.
- The nested for loop (line 16) is used to check whether the digits in the first number is unique or not. If the digits are not unique we come out of the nested for loop using the
`goto`

statement (line 28) and move on to the next number. - If the digits in the first number are unique then the control comes to the for loop defined in line 33. This for loop checks whether the given three numbers have unique digits or not. If digits are unique then the numbers are triad numbers, otherwise, we come out of the nested for loop using the
`goto`

statement and check for the next number.

Load Comments