# Strobogrammatic number

• Difficulty Level : Medium
• Last Updated : 08 Aug, 2022

For the given length n, find all n-length Strobogrammatic numbers.

Strobogrammatic Number is a number whose numeral is rotationally symmetric so that it appears the same when rotated 180 degrees. In other words, Strobogrammatic Number appears the same right-side up and upside down.

0 after 180Â° rotation : (0 â†’ 0)
1 after 180Â° rotation : (1 â†’ 1)
8 after 180Â° rotation : (8 â†’ 8)
6 after 180Â° rotation : (6 â†’ 9)
9 after 180Â° rotation : (9 â†’ 6)

Examples :

Input : n = 2
Output : 88  11  96  69

Input : n = 4
Output : 8008 1001 9006 6009 8888 1881 9886 6889 8118 1111
9116 6119 8968 1961 9966 6969 8698 1691 9696 6699

Below is the Python3 implementation :

## C++

 `// C++ program to print all` `// Strobogrammatic number of length n` `#include ` `using` `namespace` `std;`   `// definition function` `vector numdef(``int` `n, ``int` `length)` `{` `    ``vector result;` `    ``if` `(n == 0)` `        ``return` `result;` `    ``if` `(n == 1) {` `        ``result.push_back(``"1"``);` `        ``result.push_back(``"0"``);` `        ``result.push_back(``"8"``);` `        ``return` `result;` `    ``}`   `    ``vector middles = numdef(n - 2, length);`   `    ``for` `(string middle : middles) {` `        ``if` `(n != length)` `            ``result.push_back(``"0"` `+ middle + ``"0"``);`   `        ``result.push_back(``"8"` `+ middle + ``"8"``);` `        ``result.push_back(``"1"` `+ middle + ``"1"``);` `        ``result.push_back(``"9"` `+ middle + ``"6"``);` `        ``result.push_back(``"6"` `+ middle + ``"9"``);` `    ``}` `    ``return` `result;` `}`   `// strobogrammatic function` `vector strobogrammatic_num(``int` `n)` `{` `    ``vector result = numdef(n, n);` `    ``return` `result;` `}`   `// Driver Code` `int` `main()` `{` `    ``// Print all Strobogrammatic` `    ``// number for n = 3` `    ``for` `(string num : (strobogrammatic_num(3)))` `        ``cout << num << ``" "``;` `}`   `// This code is contributed by phasing17`

## Java

 `// Java program to print all` `// Strobogrammatic number of length n` `import` `java.util.*;`   `class` `GFG {`   `  ``// definition function` `  ``static` `ArrayList numdef(``int` `n, ``int` `length)` `  ``{` `    ``ArrayList result = ``new` `ArrayList();` `    ``if` `(n == ``0``)` `      ``return` `result;` `    ``if` `(n == ``1``) {` `      ``result.add(``"1"``);` `      ``result.add(``"0"``);` `      ``result.add(``"8"``);` `      ``return` `result;` `    ``}`   `    ``ArrayList middles = numdef(n - ``2``, length);`   `    ``for` `(String middle : middles) {` `      ``if` `(n != length)` `        ``result.add(``"0"` `+ middle + ``"0"``);`   `      ``result.add(``"8"` `+ middle + ``"8"``);` `      ``result.add(``"1"` `+ middle + ``"1"``);` `      ``result.add(``"9"` `+ middle + ``"6"``);` `      ``result.add(``"6"` `+ middle + ``"9"``);` `    ``}` `    ``return` `result;` `  ``}`   `  ``// strobogrammatic function` `  ``static` `ArrayList strobogrammatic_num(``int` `n)` `  ``{` `    ``ArrayList result = numdef(n, n);` `    ``return` `result;` `  ``}`   `  ``// Driver Code` `  ``public` `static` `void` `main(String[] args)` `  ``{` `    ``// Print all Strobogrammatic` `    ``// number for n = 3` `    ``for` `(String num : (strobogrammatic_num(``3``)))` `      ``System.out.print(num + ``" "``);` `  ``}` `}`   `// This code is contributed by phasing17`

## Python3

 `# Python program to print all` `# Strobogrammatic number of length n` `  `  `# strobogrammatic function ` `def` `strobogrammatic_num(n):` `      `  `    ``result ``=` `numdef(n, n)` `    ``return` `result` `      `  `# definition function` `def` `numdef(n, length):` `      `  `    ``if` `n ``=``=` `0``: ``return` `[""]` `    ``if` `n ``=``=` `1``: ``return` `[``"1"``, ``"0"``, ``"8"``]` `      `  `    ``middles ``=` `numdef(n ``-` `2``, length)` `    ``result ``=` `[]` `      `  `    ``for` `middle ``in` `middles:` `        ``if` `n !``=` `length:            ` `            ``result.append(``"0"` `+` `middle ``+` `"0"``)` `  `  `        ``result.append(``"8"` `+` `middle ``+` `"8"``)` `        ``result.append(``"1"` `+` `middle ``+` `"1"``)` `        ``result.append(``"9"` `+` `middle ``+` `"6"``)` `        ``result.append(``"6"` `+` `middle ``+` `"9"``)` `    ``return` `result` `  `  `# Driver Code` `if` `__name__ ``=``=` `'__main__'``:` `      `  `    ``# Print all Strobogrammatic ` `    ``# number for n = 3` `    ``print``(strobogrammatic_num(``3``))`

## C#

 `// C# program to print all` `// Strobogrammatic number of length n` `using` `System;` `using` `System.Collections.Generic;`   `class` `GFG ` `{`   `  ``// definition function` `  ``static` `List<``string``> numdef(``int` `n, ``int` `length)` `  ``{` `    ``List<``string``> result = ``new` `List<``string``>();` `    ``if` `(n == 0)` `      ``return` `result;` `    ``if` `(n == 1) {` `      ``result.Add(``"1"``);` `      ``result.Add(``"0"``);` `      ``result.Add(``"8"``);` `      ``return` `result;` `    ``}`   `    ``List<``string``> middles = numdef(n - 2, length);`   `    ``foreach``(``string` `middle ``in` `middles)` `    ``{` `      ``if` `(n != length)` `        ``result.Add(``"0"` `+ middle + ``"0"``);`   `      ``result.Add(``"8"` `+ middle + ``"8"``);` `      ``result.Add(``"1"` `+ middle + ``"1"``);` `      ``result.Add(``"9"` `+ middle + ``"6"``);` `      ``result.Add(``"6"` `+ middle + ``"9"``);` `    ``}` `    ``return` `result;` `  ``}`   `  ``// strobogrammatic function` `  ``static` `List<``string``> strobogrammatic_num(``int` `n)` `  ``{` `    ``List<``string``> result = numdef(n, n);` `    ``return` `result;` `  ``}`   `  ``// Driver Code` `  ``public` `static` `void` `Main(``string``[] args)` `  ``{` `    ``// Print all Strobogrammatic` `    ``// number for n = 3` `    ``foreach``(``string` `num ``in``(strobogrammatic_num(3)))` `      ``Console.Write(num + ``" "``);` `  ``}` `}`   `// This code is contributed by phasing17`

## Javascript

 `// JavaScript program to print all` `// Strobogrammatic number of length n` `  `  `// strobogrammatic function ` `function` `strobogrammatic_num(n)` `{` `    ``let result = numdef(n, n)` `    ``return` `result` `}`   `// definition function` `function` `numdef(n, length)` `{      ` `    ``if` `(n == 0) ``return` `[``""``]` `    ``if` `(n == 1) ``return` `[``"1"``, ``"0"``, ``"8"``]` `      `  `    ``let middles = numdef(n - 2, length)` `    ``let result = []` `      `  `    ``for` `(``var` `middle of middles)` `    ``{` `        ``if` `(n != length)           ` `            ``result.push(``"0"` `+ middle + ``"0"``)` `  `  `        ``result.push(``"8"` `+ middle + ``"8"``)` `        ``result.push(``"1"` `+ middle + ``"1"``)` `        ``result.push(``"9"` `+ middle + ``"6"``)` `        ``result.push(``"6"` `+ middle + ``"9"``)` `    ``}` `    ``return` `result` `}` ` `  `  `  `// Driver Code`   `// Print all Strobogrammatic ` `// number for n = 3` `console.log(strobogrammatic_num(3))`       `// This code is contributed by phasing17`

Output :

[‘818’, ‘111’, ‘916’, ‘619’, ‘808’, ‘101’, ‘906’, ‘609’, ‘888’, ‘181’, ‘986’, ‘689’]

My Personal Notes arrow_drop_up
Related Articles