# Check if the number is valid when flipped upside down

Given a string str representing a number, the task is to find whether the number is valid or not if it made topsy-turvy i.e upside-down.

Examples:

Input: str = “1183”
Output: Yes
upside-down(1183) = 1183

Input: str = “983”
Output: No

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

Approach: Only the digits 1, 3 and 8 are the digits that can form another valid digit when turned upside-down. If the number contains a digit other than these then print No else print Yes.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function that returns true if ` `// str is Topsy Turvy ` `bool` `topsyTurvy(string str) ` `{ ` ` `  `    ``// For every character of the string ` `    ``for` `(``int` `i = 0; i < str.length(); i++) { ` ` `  `        ``// If the current digit cannot form a ` `        ``// valid digit when turned upside-down ` `        ``if` `(str[i] == ``'2'` `|| str[i] == ``'4'` `            ``|| str[i] == ``'5'` `|| str[i] == ``'6'` `            ``|| str[i] == ``'7'` `|| str[i] == ``'9'``) { ` `            ``return` `false``; ` `        ``} ` `    ``} ` ` `  `    ``return` `true``; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``string str = ``"1234"``; ` ` `  `    ``if` `(topsyTurvy(str)) ` `        ``cout << ``"Yes"``; ` `    ``else` `        ``cout << ``"No"``; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `import` `java.util.*; ` ` `  `class` `GFG  ` `{ ` ` `  `// Function that returns true if ` `// str is Topsy Turvy ` `static` `boolean` `topsyTurvy(``char``[] str) ` `{ ` ` `  `    ``// For every character of the string ` `    ``for` `(``int` `i = ``0``; i < str.length; i++)  ` `    ``{ ` ` `  `        ``// If the current digit cannot form a ` `        ``// valid digit when turned upside-down ` `        ``if` `(str[i] == ``'2'` `|| str[i] == ``'4'` `||  ` `            ``str[i] == ``'5'` `|| str[i] == ``'6'` `||  ` `            ``str[i] == ``'7'` `|| str[i] == ``'9'``) ` `        ``{ ` `            ``return` `false``; ` `        ``} ` `    ``} ` `    ``return` `true``; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args)  ` `{ ` `    ``String str = ``"1234"``; ` ` `  `    ``if` `(topsyTurvy(str.toCharArray())) ` `        ``System.out.println(``"Yes"``); ` `    ``else` `        ``System.out.println(``"No"``); ` `} ` `}  ` ` `  `// This code is contributed by Rajput-Ji `

## Python3

 `# Python3 implementation of the approach  ` ` `  `# Function that returns true if  ` `# str is Topsy Turvy  ` `def` `topsyTurvy(string) :  ` ` `  `    ``# For every character of the string  ` `    ``for` `i ``in` `range``(``len``(string)) : ` `         `  `        ``# If the current digit cannot form a  ` `        ``# valid digit when turned upside-down  ` `        ``if` `(string[i] ``=``=` `'2'` `or` `string[i] ``=``=` `'4'` `or`  `            ``string[i] ``=``=` `'5'` `or` `string[i] ``=``=` `'6'` `or`  `            ``string[i] ``=``=` `'7'` `or` `string[i] ``=``=` `'9'``) : ` `            ``return` `False``;  ` `             `  `    ``return` `True``;  ` ` `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"` `:  ` ` `  `    ``string ``=` `"1234"``;  ` ` `  `    ``if` `(topsyTurvy(string)) : ` `        ``print``(``"Yes"``);  ` `    ``else` `: ` `        ``print``(``"No"``);  ` ` `  `# This code is contributed by AnkitRai01 `

## C#

 `// C# implementation of the approach ` `using` `System; ` `class` `GFG  ` `{ ` ` `  `// Function that returns true if ` `// str is Topsy Turvy ` `static` `bool` `topsyTurvy(``char``[] str) ` `{ ` ` `  `    ``// For every character of the string ` `    ``for` `(``int` `i = 0; i < str.Length; i++)  ` `    ``{ ` ` `  `        ``// If the current digit cannot form a ` `        ``// valid digit when turned upside-down ` `        ``if` `(str[i] == ``'2'` `|| str[i] == ``'4'` `||  ` `            ``str[i] == ``'5'` `|| str[i] == ``'6'` `||  ` `            ``str[i] == ``'7'` `|| str[i] == ``'9'``) ` `        ``{ ` `            ``return` `false``; ` `        ``} ` `    ``} ` `    ``return` `true``; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String[] args)  ` `{ ` `    ``String str = ``"1234"``; ` ` `  `    ``if` `(topsyTurvy(str.ToCharArray())) ` `        ``Console.WriteLine(``"Yes"``); ` `    ``else` `        ``Console.WriteLine(``"No"``); ` `} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

Output:

```No
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

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.