Write a program to find the four digited numbers, which are perfect squares, and all the digits in that number are even.
Answers
Answer:
class SquareFourDigit {
static boolean perfectSquare(double x) {
double sq = Math.sqrt(x);
return ((sq - Math.floor(sq)) == 0);
}
public static void main(String[] args) {
int count = 0, flag = 0;
for (int h = 2000; h <= 8888; h = h + 2000) {
for (int i = h; i <= h + 800; i = i + 200) {
for (int j = i; j <= i + 88; j = j + 20) {
for (int k = j; k <= j + 8; k = k + 2) {
if (perfectSquare(k)) {
System.out.println(k);
}
}
}
}
}
}
}
Explanation:
there are 4 loops in this program
the first loop starts from 2000(least 4 digit number with even digits) and increments by 2000 until 8888(greatest 4 digit number with all even digits)
similarly
the second loop increments by 200
the third loop increments by 20
the fourth loop increments by 2.
sincle the even digit series can be
2000,2002,2004,2006,2008, (increments by 2)
2020,2022,2024,2026
the above two lines has in increment of 20
2200
(increment by 200)
.
.
.
2800
(increments by 2000)
4000