Given an integer N, the task is to check if the given number is a perfect square having all its digits as a perfect square or not. If found to be true, then print “Yes”. Otherwise, print “No”.
Examples:
Input: N = 144
Output: Yes
Explanation:
The number 144 is a perfect square and also the digits of the number {1(= 12, 4(= 22} is also a perfect squares.
Input: N = 81
Output: No
Approach: The idea is to check if the given number N is a perfect square or not. If found to be true, check if all its digits are either 0, 1, 4, or 9. If found to be true, print “Yes”. Otherwise, print “No”.
Below is the implementation of the above approach:
C++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;// Function to check if digits of// N is a perfect square or notbool check_digits(long N){ // Iterate over the digits while (N > 0) { // Extract the digit int n = N % 10; // Check if digit is a // perfect square or not if ((n != 0) && (n != 1) && (n != 4) && (n != 9)) { return 0; } // Divide N by 10 N = N / 10; } // Return true return 1;}// Function to check if N is// a perfect square or notbool is_perfect(long N){ long double n = sqrt(N); // If floor and ceil of n // is not same if (floor(n) != ceil(n)) { return 0; } return 1;}// Function to check if N satisfies// the required conditions or notvoid isFullSquare(long N){ // If both the conditions // are satisfied if (is_perfect(N) && check_digits(N)) { cout << "Yes"; } else { cout << "No"; }}// Driver Codeint main(){ long N = 144; // Function Call isFullSquare(N); return 0;} |
Java
// Java program for// the above approachimport java.util.*;class GFG{// Function to check if digits of// N is a perfect square or notstatic boolean check_digits(long N){ // Iterate over the digits while (N > 0) { // Extract the digit int n = (int) (N % 10); // Check if digit is a // perfect square or not if ((n != 0) && (n != 1) && (n != 4) && (n != 9)) { return false; } // Divide N by 10 N = N / 10; } // Return true return true;}// Function to check if N is// a perfect square or notstatic boolean is_perfect(long N){ double n = Math.sqrt(N); // If floor and ceil of n // is not same if (Math.floor(n) != Math.ceil(n)) { return false; } return true;}// Function to check if N satisfies// the required conditions or notstatic void isFullSquare(long N){ // If both the conditions // are satisfied if (is_perfect(N) && check_digits(N)) { System.out.print("Yes"); } else { System.out.print("No"); }}// Driver Codepublic static void main(String[] args){ long N = 144; // Function Call isFullSquare(N);}}// This code is contributed by Rajput-Ji |
Python3
# Python3 program for the above approachimport math# Function to check if digits of# N is a perfect square or notdef check_digits(N): # Iterate over the digits while (N > 0): # Extract the digit n = N % 10 # Check if digit is a # perfect square or not if ((n != 0) and (n != 1) and (n != 4) and (n != 9)): return 0 # Divide N by 10 N = N // 10 # Return true return 1# Function to check if N is# a perfect square or notdef is_perfect(N): n = math.sqrt(N) # If floor and ceil of n # is not same if (math.floor(n) != math.ceil(n)): return 0 return 1# Function to check if N satisfies# the required conditions or notdef isFullSquare(N): # If both the conditions # are satisfied if (is_perfect(N) and check_digits(N)): print("Yes") else: print("No") # Driver CodeN = 144# Function callisFullSquare(N)# This code is contributed by sanjoy_62 |
C#
// C# program for// the above approachusing System;class GFG{// Function to check if digits of// N is a perfect square or notstatic bool check_digits(long N){ // Iterate over the digits while (N > 0) { // Extract the digit int n = (int) (N % 10); // Check if digit is a // perfect square or not if ((n != 0) && (n != 1) && (n != 4) && (n != 9)) { return false; } // Divide N by 10 N = N / 10; } // Return true return true;}// Function to check if N is// a perfect square or notstatic bool is_perfect(long N){ double n = Math.Sqrt(N); // If floor and ceil of n // is not same if (Math.Floor(n) != Math.Ceiling(n)) { return false; } return true;}// Function to check if N satisfies// the required conditions or notstatic void isFullSquare(long N){ // If both the conditions // are satisfied if (is_perfect(N) && check_digits(N)) { Console.Write("Yes"); } else { Console.Write("No"); }}// Driver Codepublic static void Main(){ long N = 144; // Function Call isFullSquare(N);}}// This code is contributed by Chitranayal |
Javascript
<script>// JavaScript program for the above approach// Function to check if digits of// N is a perfect square or notfunction check_digits(N){ // Iterate over the digits while (N > 0) { // Extract the digit let n = N % 10; // Check if digit is a // perfect square or not if ((n != 0) && (n != 1) && (n != 4) && (n != 9)) { return 0; } // Divide N by 10 N = Math.floor(N / 10); } // Return true return 1;}// Function to check if N is// a perfect square or notfunction is_perfect(N){ let n = Math.sqrt(N); // If floor and ceil of n // is not same if (Math.floor(n) != Math.ceil(n)) { return 0; } return 1;}// Function to check if N satisfies// the required conditions or notfunction isFullSquare(N){ // If both the conditions // are satisfied if (is_perfect(N) && check_digits(N)) { document.write("Yes"); } else { document.write("No"); }}// Driver Code let N = 144; // Function Call isFullSquare(N);// This code is contributed by Surbhi Tyagi.</script> |
Yes
Time Complexity: O(log10N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.


