Self Descriptive Number

A self-descriptive number is an integer n in given base b is b digits long in which each digit at position p (the most significant digit being at position 0 and the least significant at position b – 1) counts how many times a digit p is in n.
For example in base 10, 6210001000 is a self descriptive number .
Explanation :
It is 10 digit number in base 10.
It has 6 at the position 0 and there are six 0s in 6210001000.
It has 2 at the position 1 and there are two 1s in 6210001000.
It has 1 at the position 2 and there is one 2s in 6210001000.
It has 0 at the position 3 and there are zero 3s in 6210001000.
It has 0 at the position 4 and there are zero 4s in 6210001000.
It has 0 at the position 5 and there are zero 5s in 6210001000.
It has 1 at the position 6 and there is one 6 in 6210001000.
It has 0 at the position 7 and there are zero 7s in 6210001000.
It has 0 at the position 8 and there are zero 8s in 6210001000.
It has 0 at the position 9 and there are zero 9s in 6210001000.

[Source : Wikipedia]

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Here is a program to print all self-descriptive numbers below 100000000. In the below program we have just ignored one fact about the self-descriptive number that it should have as many number of digits as much the base is given.

Description of Program :
1 . Firstly all the digits get extracted from the outer loop and are stored in a variable b in each iteration.
2 . Then in the inner loop there is a count on how many times number i (this i is ith index of outer loop) is present in the string .
3 . Finally that count is compared with the digit present at the ith index stored in variable b.

C++

 // C++ program to print  // all self descriptive // number below 100000000 #include using namespace std;    bool isSelfDescriptiveNumber(int num) {     // converting the integer     // num to string     string s = to_string(num);     for (int i = 0;               i < s.size(); i++)      {            // Extracting each digit         // one by one from the          // string         char temp_char = s.at(i);            // converting the string          // (digit) into integer b         // variable stores the digit         // present at index 'i'         int b = temp_char - '0';            // counting how many          // times the particular          // digit occur in the         // whole number "num"         int count = 0;         for (int j = 0;                  j < s.size(); j++)          {             // converting string             // char to integer             int temp = s.at(j) - '0';                // checking whether it is              // equal to the index 'i'             // if it is then increment             // the count .             if (temp == i)              {                 count++;             }         }                    // If it is not equal         // it return false .         if (count != b)             return false;     }     return true; }    // Driver Code int main() {     for (int i = 0; i < 100000000; i++)          if (isSelfDescriptiveNumber(i))              cout << i << endl;  }    // This code is contributed by  // Manish Shaw(manishshaw1)

Java

 // Java program to print all self descriptive // number below 100000000 public class SelfDescriptive {        public static boolean isSelfDescriptiveNumber(int num)     {         // converting the integer num to string         String s = Integer.toString(num);         for (int i = 0; i < s.length(); i++) {                // Extracting each digit one by one from the string             String temp_char = s.charAt(i) + "";                // converting the string (digit) into integer             // b variable stores the digit present at index 'i'             int b = Integer.parseInt(temp_char);                // counting how many times the particular digit             // occur in the whole number "num"             int count = 0;             for (int j = 0; j < s.length(); j++) {                 // converting string char to integer                 int temp = Integer.parseInt(s.charAt(j) + "");                    // checking whether it is equal to the index 'i'                 // if it is then increment the count .                 if (temp == i) {                     count++;                 }             }                // If it is not equal             // it return false .             if (count != b)                 return false;         }         return true;     }        public static void main(String[] args)     {         for (int i = 0; i < 100000000; i++)              if (isSelfDescriptiveNumber(i))                  System.out.println(i);             } }

C#

 // C# program to print  // all self descriptive // number below 100000000 using System;     class GFG  {  static bool isSelfDescriptiveNumber(int num) {     // converting the integer     // num to string     string s = num.ToString();     for (int i = 0;               i < s.Length; i++)      {            // Extracting each digit         // one by one from the          // string         string temp_char = s[i] + "";            // converting the string          // (digit) into integer b         // variable stores the digit         // present at index 'i'         int b = int.Parse(temp_char);            // counting how many          // times the particular          // digit occur in the         // whole number "num"         int count = 0;         for (int j = 0;                  j < s.Length; j++)          {             // converting string             // char to integer             int temp = int.Parse(s[j] + "");                // checking whether it is              // equal to the index 'i'             // if it is then increment             // the count .             if (temp == i)              {                 count++;             }         }                     // If it is not equal         // it return false .         if (count != b)             return false;     }     return true; }     // Driver Code static void Main() {     for (int i = 0; i < 100000000; i++)          if (isSelfDescriptiveNumber(i))              Console.WriteLine(i);     } }     // This code is contributed by  // Manish Shaw(manishshaw1)

PHP



Output:

1210
2020
21200
3211000

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.