The program must accept an integer N as the input. The task is to check whether all the individual digits in the number are perfect squares. If they are, the program should print YES. Otherwise, it should print NO.
Boundary Condition(s):
1 <= N <= 10^8
Input Format:
The first line contains the number N.
Output Format:
The first line contains either YES or NO.
Example Input/Output 1:
Input:
149
Output:
YES
Explanation:
All the digits in the number (1, 4, 9) are perfect squares.
Example Input/Output 2:
Input:
165
Output:
NO
Explanation:
Though 1 and 4 are perfect squares, 6 is not a perfect square.
n = input().strip()
perfect_squares = {'0', '1', '4', '9'}
if all(digit in perfect_squares for digit in n):
print("YES")
else:
print("NO")
#include <stdio.h>
int main() {
char num[10];
scanf("%s", num);
char perfect_squares[] = "0149";
int flag = 1;
for(int i = 0; num[i] != ' '; i++) {
if(!strchr(perfect_squares, num[i])) {
flag = 0;
break;
}
}
if(flag) {
printf("YES");
} else {
printf("NO");
}
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main() {
string num;
cin >> num;
string perfect_squares = "0149";
bool allPerfect = true;
for(char c : num) {
if(perfect_squares.find(c) == string::npos) {
allPerfect = false;
break;
}
}
cout << (allPerfect ? "YES" : "NO");
return 0;
}
import java.util.Scanner;
public class PerfectSquareDigits {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String num = sc.next();
String perfectSquares = "0149";
boolean allPerfect = true;
for(char c : num.toCharArray()) {
if(perfectSquares.indexOf(c) == -1) {
allPerfect = false;
break;
}
}
System.out.println(allPerfect ? "YES" : "NO");
}
}