Check if the number is valid when flipped upside down - GeeksforGeeks

18 Mar, 2022
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) = 1183Input: str = “983” Output: No  
 
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++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function that returns true if// str is Topsy Turvybool 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 codeint main(){    string str = "1234";     if (topsyTurvy(str))        cout << "Yes";    else        cout << "No";     return 0;}
// Java implementation of the approachimport java.util.*; class GFG{ // Function that returns true if// str is Topsy Turvystatic 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 codepublic 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 implementation of the approach # Function that returns true if# str is Topsy Turvydef 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 codeif __name__ == "__main__" :     string = "1234";     if (topsyTurvy(string)) :        print("Yes");    else :        print("No"); # This code is contributed by AnkitRai01
// C# implementation of the approachusing System;class GFG{ // Function that returns true if// str is Topsy Turvystatic 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 codepublic 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
<script> // Javascript implementation of the approach // Function that returns true if// str is Topsy Turvyfunction topsyTurvy( str){     // For every character of the string    for (var 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 codevar str = "1234";if (topsyTurvy(str))    document.write( "Yes");else    document.write( "No"); </script>
No
 
Time Complexity: O(|str|)
Auxiliary Space: O(1)
ankthon
Rajput-Ji
29AjayKumar
itsok
subhamkumarm348
Numbers
Mathematical
Mathematical
Numbers
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Modulo Operator (%) in C/C++ with Examples
Merge two sorted arrays
Prime Numbers
Program to find sum of elements in a given array
Program for factorial of a number
Operators in C / C++
Euclidean algorithms (Basic and Extended)
Algorithm to solve Rubik's Cube
The Knight's tour problem | Backtracking-1
Minimum number of jumps to reach end