Table of contents
1.
Introduction 
2.
Explanation of Sunny Numbers
3.
Checking if a Number is a Sunny Number in Java:
3.1.
Java
4.
Examples
4.1.
Example 1
4.2.
Example 2
4.3.
Example 3
5.
Frequently Asked Questions
5.1.
Are there any negative sunny numbers?
5.2.
Is 0 a sunny number?
5.3.
How many sunny numbers are there?
6.
Conclusion
Last Updated: Aug 14, 2024
Easy

Sunny Number in Java

Author Ravi Khorwal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 

A sunny number is a special type of number in Java. It is a positive number where the sum of its digits equals the number itself when those digits are squared. For example, 19 is a sunny number because 1^2 + 9^2 = 1 + 81 = 82, which equals 19. 

Sunny Number in Java

In this article, we will discuss what sunny numbers are, how to check if a number is a sunny number, and will see its code implementation in Java.

Explanation of Sunny Numbers

A sunny number is a positive integer where the sum of the squares of its individual digits equals the number itself. 

Let's discuss this step by step:

1. Take a positive integer, such as 19.
 

2. Separate the digits of the number: 1 and 9.
 

3. Square each digit: 1^2 = 1, and 9^2 = 81.
 

4. Add the squared digits together: 1 + 81 = 82.
 

5. If the sum of the squared digits equals the original number, it is a sunny number.
 

In the case of 19, we have 1^2 + 9^2 = 82, which is equal to 19. Therefore, 19 is a sunny number.


Another example is the number 100. Let's check if it is a sunny number:

1^2 + 0^2 + 0^2 = 1 + 0 + 0 = 1, which is not equal to 100. So, 100 is not a sunny number.


Sunny numbers are an interesting mathematical concept that can be explored and implemented in programming languages like Java.

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

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.

Live masterclass