Check if a given number is Fancy - GeeksforGeeks

11 Jun, 2021
A fancy number is one which when rotated 180 degrees is the same. Given a number, find whether it is fancy or not.180 degree rotations of 6, 9, 1, 0 and 8 are 9, 6, 1, 0 and 8 respectivelyExamples:  
Input:  num =  96
Output: Yes
If we rotate given number by 180, we get same number

Input:  num =  916
Output: Yes
If we rotate given number by 180, we get same number

Input:  num =  996
Output: No

Input:  num =  121
Output: No
We strongly recommend you to minimize your browser and try this yourself first.The idea is to create a map to store fancy pair mappings. After creating map, traverse given number from both ends and if at any point characters at current ends are not fancy pairs, return false. This algorithm is similar to palindrome check algorithm.Below is the implementation of above idea.  
C++
Java
C#
Python3
Javascript
// C++ program to find if a given number is fancy or not.#include<bits/stdc++.h>using namespace std; bool isFancy(string& num){    // To store mappings of fancy pair characters. For example    // 6 is paired with 9 and 9 is paired with 6.    map<char, char> fp;    fp['0'] = '0';    fp['1'] = '1';    fp['6'] = '9';    fp['8'] = '8';    fp['9'] = '6';     // Find number of digits in given number    int n = num.length();     // Traverse from both ends, and compare characters one by one    int l = 0, r = n-1;    while (l<=r)    {        // If current characters at both ends are not fancy pairs        if (fp.find(num[l]) == fp.end() || fp[num[l]] != num[r])            return false;        l++;        r--;    }    return true;} // Driver programint main(){    string str = "9088806";    isFancy(str)? cout << "Yes": cout << "No";    return 0;}
// Java program to find if a given number// is fancy or notimport java.util.*; class GFG{static boolean isFancy(String num){    // To store mappings of fancy pair characters.    // For example 6 is paired with 9 and 9 is paired with 6.    Map<Character,        Character> fp = new HashMap<Character,                                    Character>();                                         fp. put('0', '0');    fp. put('1', '1');    fp. put('6', '9');    fp. put('8', '8');    fp. put('9', '6');     // Find number of digits in given number    int n = num.length();     // Traverse from both ends,    // and compare characters one by one    int l = 0, r = n-1;    while (l <= r)    {        // If current characters at both ends        // are not fancy pairs        if (!fp.containsKey(num.charAt(l)) ||             fp.get(num.charAt(l)) != num.charAt(r))            return false;        l++;        r--;    }    return true;} // Driver Codepublic static void main(String[] args){    String str = "9088806";    if(isFancy(str))        System.out.println("Yes");    else        System.out.println("No");}} // This code is contributed by PrinciRaj1992
// C# program to find if a given number// is fancy or notusing System;using System.Collections.Generic; class GFG{static bool isFancy(String num){    // To store mappings of fancy pair characters.    // For example 6 is paired with 9 and 9 is paired with 6.    Dictionary<char, char> fp = new Dictionary<char, char>();                                         fp.Add('0', '0');    fp.Add('1', '1');    fp.Add('6', '9');    fp.Add('8', '8');    fp.Add('9', '6');     // Find number of digits in given number    int n = num.Length;     // Traverse from both ends,    // and compare characters one by one    int l = 0, r = n - 1;    while (l <= r)    {        // If current characters at both ends        // are not fancy pairs        if (!fp.ContainsKey(num[l]) ||             fp[num[l]] != num[r])            return false;        l++;        r--;    }    return true;} // Driver Codepublic static void Main(String[] args){    String str = "9088806";    if(isFancy(str))        Console.WriteLine("Yes");    else        Console.WriteLine("No");}} // This code is contributed by Rajput-Ji
# Python 3 program to find if a given number is fancy or not.def isFancy(num):     # To store mappings of fancy pair characters. For example    # 6 is paired with 9 and 9 is paired with 6.    fp = {}    fp['0'] = '0'    fp['1'] = '1'    fp['6'] = '9'    fp['8'] = '8'    fp['9'] = '6'     # Find number of digits in given number    n = len(num)     # Traverse from both ends, and compare characters one by one    l = 0    r = n-1    while (l <= r):         # If current characters at both ends are not fancy pairs        if (num[l] not in fp or fp[num[l]] != num[r]):            return False        l += 1        r -= 1    return True # Driver programif __name__ == "__main__":     st = "9088806"    if isFancy(st):        print("Yes")    else:        print("No") # This code is contributed by ukasp.
<script>// javascript program to find if a given number// is fancy or not    function isFancy( num)    {             // To store mappings of fancy pair characters.        // For example 6 is paired with 9 and 9 is paired with 6.        var fp = new Map();         fp.set('0', '0');        fp.set('1', '1');        fp.set('6', '9');        fp.set('8', '8');        fp.set('9', '6');         // Find number of digits in given number        var n = num.length;         // Traverse from both ends,        // and compare characters one by one        var l = 0, r = n - 1;        while (l <= r) {            // If current characters at both ends            // are not fancy pairs            if (!fp.has(num.charAt(l)) || fp.get(num.charAt(l)) != num.charAt(r))                return false;            l++;            r--;        }        return true;    }     // Driver Code             var str = "9088806";        if (isFancy(str))            document.write("Yes");        else            document.write("No"); // This code contributed by aashish1995</script>
Output:  
Yes
Thanks to Gaurav Ahirwar for suggesting above solution.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
princiraj1992
Rajput-Ji
aashish1995
ukasp
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Algorithm to solve Rubik's Cube
Program to print prime numbers from 1 to N.
Fizz Buzz Implementation
Program to multiply two matrices
Modular multiplicative inverse
Check if a number is Palindrome
Find first and last digits of a number
Count ways to reach the n'th stair
Program to convert a given number to words
Find Union and Intersection of two unsorted arrays