Check whether the number formed by concatenating two numbers is a perfect square or not

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

Examples:

Input: a = 1, b = 21
Output: Yes
121 = 11 × 11, is a perfect square.

Input: a = 100, b = 100
Output: No
100100 is not a perfect square.



Approach: Initialize the number as strings initially and concatenate them. Convert the string to a number using Integer.valueOf() function. Once the string has been converted to a number, check if the number is a perfect square 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 square or not
#include <bits/stdc++.h>
using namespace std;
  
// Function to check if
// the concatenation is
// a perfect square
void checkSquare(string s1, string s2)
{
  
    // Function to convert 
    // concatenation of 
    // strings to a number
    int c = stoi(s1 + s2);
  
    // square root of number
    int d = sqrt(c);
  
    // check if it is a
    // perfect square
    if (d * d == c) 
    {
        cout << "Yes";
    }
    else 
    {
        cout << "No";
    }
}
  
// Driver Code
int main()
{
    string s1 = "12";
    string s2 = "1";
  
    checkSquare(s1, s2);
      
    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 square or not
import java.lang.*;
class GFG {
  
    // Function to check if the concatenation is
    // a perfect square
    static void checkSquare(String s1, String s2)
    {
  
        // Function to convert concatenation
        // of strings to a number
        int c = Integer.valueOf(s1 + s2);
  
        // square root of number
        int d = (int)Math.sqrt(c);
  
        // check if it is a perfect square
        if (d * d == c) {
            System.out.println("Yes");
        }
        else {
            System.out.println("No");
        }
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        String s1 = "12";
        String s2 = "1";
  
        checkSquare(s1, s2);
    }
}

chevron_right


Python 3

# Python 3 program to check if the
# concatenation of two numbers
# is a perfect square or not
import math

# Function to check if the concatenation
# is a perfect square
def checkSquare(s1, s2):

# Function to convert concatenation of
# strings to a number
c = int(s1 + s2)

# square root of number
d = math.sqrt(c)

# check if it is a perfect square
if (d * d == c) :
print(“Yes”)

else:
print(“No”)

# Driver Code
if __name__ == “__main__”:

s1 = “12”
s2 = “1”

checkSquare(s1, s2)

# This code is contributed by ita_c

C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to check if the 
// concatenation of two numbers 
// is a perfect square or not 
using System; 
public class GFG { 
  
    // Function to check if the concatenation is 
    // a perfect square 
    static void checkSquare(String s1, String s2) 
    
  
        // Function to convert concatenation 
        // of strings to a number 
        int c = Convert.ToInt32(s1 + s2 );//int.ValueOf(s1 + s2); 
  
        // square root of number 
        int d = (int)Math.Sqrt(c); 
  
        // check if it is a perfect square 
        if (d * d == c) { 
            Console.WriteLine("Yes"); 
        
        else
            Console.WriteLine("No"); 
        
    
  
    // Driver Code 
    public static void Main() 
    
        String s1 = "12"
        String s2 = "1"
  
        checkSquare(s1, s2); 
    
  
// This code is contributed by PrinciRaj1992

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
// PHP program to check if the
// concatenation of two numbers
// is a perfect square or not
  
// Function to check if the 
// concatenation is a perfect square
function checkSquare($s1, $s2)
{
  
    // Function to convert concatenation 
    // of strings to a number
    $c = $s1.$s2;
  
    // square root of number
    $d = sqrt($c);
  
    // check if it is a
    // perfect square
    if ($d * $d == $c
    {
        echo "Yes";
    }
    else
    {
        echo "No";
    }
}
  
// Driver Code
$s1 = "12";
$s2 = "1";
  
checkSquare($s1, $s2);
  
// This code is contributed by Rajput-Ji
?>

chevron_right


Output:

Yes


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.



Improved By : princiraj1992, Rajput-Ji, Ita_c