C Program to print Triad Numbers

What are triad numbers?

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

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

Here are examples of tried numbers:

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

Expected Output:

How it works

Here are a few things to notice about this program:

  1. 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.
  2. 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.
  3. 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.

Leave a Comment

%d bloggers like this: