Checking if a Number is a Sunny Number in Java:
To determine whether a given number is a sunny number, we can write a Java method that takes an integer as input and returns a boolean value indicating if the number is a sunny number or not.
Let’s see the implementation :
Java
public static boolean isSunnyNumber(int number) {
int sum = 0;
int tempNumber = number;
while (tempNumber != 0) {
int digit = tempNumber % 10;
sum += digit * digit;
tempNumber /= 10;
}
return sum == number;
}

You can also try this code with Online Java Compiler
Run Code
In this Code:
1. We define a method named `isSunnyNumber` that takes an integer `number` as input and returns a boolean value.
2. We initialize two variables: `sum` to keep track of the sum of the squared digits, and `tempNumber` to store a temporary copy of the input number.
3. We start a while loop that continues as long as `tempNumber` is not equal to 0.
4. Inside the loop, we extract the last digit of `tempNumber` using the modulo operator `%` and store it in the `digit` variable.
5. We square the `digit` and add it to the `sum` variable.
6. We divide `tempNumber` by 10 to remove the last digit from the number.
7. The loop continues until all digits have been processed.
8. Finally, we compare the `sum` with the original `number`. If they are equal, the method returns `true`, indicating that the number is a sunny number. Otherwise, it returns `false`.
By calling this `isSunnyNumber` method with a given number, we can easily check whether it is a sunny number or not.
For example
int number = 19;
boolean isSunny = isSunnyNumber(number);
System.out.println(number + " is a sunny number: " + isSunny);
Output
19 is a sunny number: true
In this example, we pass the number 19 to the `isSunnyNumber` method, and it returns `true`, confirming that 19 is indeed a sunny number.
Examples
Example 1
Let's check if the number 28 is a sunny number.
int number = 28;
boolean isSunny = isSunnyNumber(number);
System.out.println(number + " is a sunny number: " + isSunny);
Output
28 is a sunny number: true
Explanation:
2^2 + 8^2 = 4 + 64 = 68, which is equal to 28. Therefore, 28 is a sunny number.
Example 2
Let's check if the number 42 is a sunny number.
int number = 42;
boolean isSunny = isSunnyNumber(number);
System.out.println(number + " is a sunny number: " + isSunny);
Output
42 is a sunny number: false
Explanation
4^2 + 2^2 = 16 + 4 = 20, which is not equal to 42. Therefore, 42 is not a sunny number.
Example 3
Let's check if the number 1 is a sunny number.
int number = 1;
boolean isSunny = isSunnyNumber(number);
System.out.println(number + " is a sunny number: " + isSunny);
Output
1 is a sunny number: true
Explanation
1^2 = 1, which is equal to 1. Therefore, 1 is a sunny number.
Frequently Asked Questions
Are there any negative sunny numbers?
No, sunny numbers are defined only for positive integers. Negative numbers cannot be sunny numbers.
Is 0 a sunny number?
No, 0 is not a sunny number. The definition of a sunny number requires the sum of the squares of its digits to be equal to the number itself, but 0^2 = 0, which is not equal to 0.
How many sunny numbers are there?
There are infinitely many sunny numbers. Some examples of sunny numbers are 1, 7, 13, 19, 23, 28, 31, 32, 44, 49, and so on.
Conclusion
In this article, we learned about sunny numbers in Java. We explained what sunny numbers are and how to check if a given number is a sunny number. Then, we provided a Java method isSunnyNumber that takes an integer as input and returns a boolean value indicating whether the number is a sunny number or not. Finally, we also looked at several examples to understand how to use the method and test different numbers.
You can also check out our other blogs on Code360.