Check if number formed by joining two Numbers is Perfect Cube

Given two numbers a and b, the task is to check whether the concatenation of a and b is a perfect cube or not.

Examples:

Input: a = 6, b = 4
Output: Yes
Concatenating 6 and 4 = 64. Since 64 (= 4 x 4 x 4) is a perfect cube, the output is Yes.

Input: a = 100, b = 1
Output: No
Concatenating 100 and 1 = 1001. Since 1001 is not a perfect cube, the output is No.

Approach: Since appending strings are comparatively easier than appending numbers, the simplest way to solve this problem is to convert the given integers into strings. Once the concatenated number has been obtained, it can be easily checked if it is a perfect cube or not.



Below is the implementation of the above approach.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to check if the
// concatenation of two numbers
// is a perfect cube or not
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to check if a number is
// a perfect Cube or not
bool isPerfectCube(int x)
{
    long double cr = round(cbrt(x));
  
    return (cr * cr * cr == x);
}
  
// Function to check if
// concatenation of two numbers
// is a perfect cube or not
void checkCube(int a, int b)
{
  
    // Convert numbers to string
    // using to_string()
    string s1 = to_string(a);
    string s2 = to_string(b);
  
    // Concatenate the numbers and
    // convert it into integer
    int c = stoi(s1 + s2);
  
    // Check if concatenated value
    // is perfect cube or not
    if (isPerfectCube(c)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
}
  
// Driver Code
int main()
{
    int a = 6;
    int b = 4;
  
    checkCube(a, b);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to check if the
// concatenation of two numbers
// is a perfect cube or not
class GFG {
      
    // Function to check if a number is
    // a perfect Cube or not
    static boolean isPerfectCube(int x)
    {
        long cr = Math.round(Math.cbrt(x));
      
        return (cr * cr * cr == x);
    }
      
    // Function to check if
    // concatenation of two numbers
    // is a perfect cube or not
    static void checkCube(int a, int b)
    {
      
        // Convert numbers to string
        // using to_string()
        String s1 = Integer.toString(a);
        String s2 = Integer.toString(b);
      
        // Concatenate the numbers and
        // convert it into integer
        int c = Integer.parseInt(s1 + s2);
      
        // Check if concatenated value
        // is perfect cube or not
        if (isPerfectCube(c)) {
                System.out.println("Yes");
        }
        else {
                System.out.println("No");
        }
    }
      
    // Driver Code
    public static void main (String[] args)
    {
        int a = 6;
        int b = 4;
      
        checkCube(a, b);   
    }
}
  
// This code is contributed by Yash_R

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to check if the
# concatenation of two numbers
# is a perfect cube or not
  
# Function to check if a number is
# a perfect Cube or not
def isPerfectCube(x):
    x = abs(x)
    return int(round(x ** (1. / 3))) ** 3 == x
  
# Function to check if
# concatenation of two numbers
# is a perfect cube or not
def checkCube(a, b):
  
    # Convert numbers to string
    # using to_string()
    s1 = str(a)
    s2 = str(b)
  
    # Concatenate the numbers and
    # convert it into integer
    c = int(s1 + s2)
  
    # Check if concatenated value
    # is perfect cube or not
    if (isPerfectCube(c)):
        print("Yes")
    else:
        print("No")
  
# Driver Code
if __name__ == '__main__':
    a = 6
    b = 4
  
    checkCube(a, b)
      
# This code is contributed by Surendra_Gangwar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to check if the
// concatenation of two numbers
// is a perfect cube or not
using System;
  
class GFG {
      
    // Function to check if a number is
    // a perfect Cube or not
    static bool isPerfectCube(int x)
    {
        double cr = Math.Round(Math.Cbrt(x));
      
        return (cr * cr * cr == x);
    }
      
    // Function to check if
    // concatenation of two numbers
    // is a perfect cube or not
    static void checkCube(int a, int b)
    {
      
        // Convert numbers to string
        // using to_string()
        string s1 = Convert.ToString(a);
        string s2 = Convert.ToString(b);
      
        // Concatenate the numbers and
        // convert it into integer
        int c = Convert.ToInt32(s1 + s2);
      
        // Check if concatenated value
        // is perfect cube or not
        if (isPerfectCube(c)) {
                Console.WriteLine("Yes");
        }
        else {
                Console.WriteLine("No");
        }
    }
      
    // Driver Code
    public static void Main ()
    {
        int a = 6;
        int b = 4;
      
        checkCube(a, b); 
    }
}
  
// This code is contributed by AbhiThakur

chevron_right


Output:

Yes

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




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.