Dados dos números a y b, la tarea es verificar si la concatenación de a y b es un cuadrado perfecto o no.
Ejemplos:
Entrada: a = 1, b = 21
Salida: Sí
121 = 11 × 11, es un cuadrado perfecto.
Entrada: a = 100, b = 100
Salida: No
100100 no es un cuadrado perfecto.
Enfoque: inicialice el número como strings inicialmente y concatene. Convierta la string en un número usando la función Integer.valueOf() . Una vez que la string se haya convertido en un número, compruebe si el número es un cuadrado perfecto o no.
A continuación se muestra la implementación del enfoque anterior.
C++
// 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; }
Java
// 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); } }
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#
// 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
PHP
<?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 ?>
Javascript
<script> // Javascript 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 let c = parseInt(s1 + s2); // square root of number let d = Math.floor(Math.sqrt(c)); // check if it is a perfect square if (d * d == c) { document.write("Yes"); } else { document.write("No"); } } // Driver Code let s1 = "12"; let s2 = "1"; checkSquare(s1, s2); // This code is contributed by avanitrachhadiya2155 </script>
Producción:
Yes
Publicación traducida automáticamente
Artículo escrito por Diksha Jain 2 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA