Sunny Number in Java

We’ll discover what sunny number in java are in this blog, along with how to write a Java program that will find them. Along with that, we will check different approaches to find whether a number is a sunny number or not?

What is the Sunny Number in Java?

Sunny Number Definition: When the number next to the one that is given is a perfect square, it is referred to as a sunny number. For instance, 49 is a happy number since it equals 13 squared and 4 + 9 equals 49. Since 8 + 1 = 9 and 81 is equal to 9 squared, it follows that 81 is also a happy number. Happy numbers and happy squares are other names for sunny numbers.

Sunny numbers are essential for programmers and mathematicians, as they provide a fun challenge and can help with problem-solving. They also have applications in number theory and cryptography, making them a useful tool for research and development.

Learn How to Peterson Number in Java, Now!

Sunny Number in Java Example:

Let’s use an example for a better understanding of the concept:

import java.util.*;
public class Main{
  public static void main(String args[]){
        //declare an int variable and initialize with a static value
        int inputNum=45;
        //declare a variable which store next value of input number
        double next=inputNum + 1;
        //Find the square root of the next number
        //store it as double value
        double square_root = Math.sqrt(next);
        //check whether the square root is a integer value or not
        //if yes return true otherwise false
        if(((square_root - Math.floor(square_root)) == 0))
        //if true then print it is a sunny number
            System.out.println(inputNum + " is a sunny number.");
            //if true then print it is a sunny number
            System.out.println(inputNum + " is not a sunny number.");

Explanation of the code:

Java code is provided to determine whether a given number is a “sunny number” or not. A sunny number is one whose square root is an integer when followed by another number. For instance, the fact that 46 is the number after 45 and that its square root is 6, an integer, makes 46 a sunny number.

The code first sets the value 45 for the integer variable inputNum. The next declaration is a double variable, which stores the following value of the input number after adding 1. The Math.sqrt() function is used to compute and store the square root of next.

Once the floor of square root has been subtracted from square root, the code checks to see if the result is zero to determine whether the square root of next is an integer or not. The code prints that the input number is a sunny number if the square root is an integer. If not, it displays a message saying that the input number is not a sunny number.

Also Learn How to Automorphic Number in Java, Now!


When the code is run, the following result is produced:

45 is a sunny number.

This is so because the number 46, which comes after 45 in a series, has a square root of 6, an integer. 45 is a sunny number as a result.

Learn How to Generate Random Numbers in Javascript, Now!

Sunny Number Example 2: checking if the square root of the number minus one is a perfect square.

import java.util.*;
public class SunnyNumberExample1
//driver code
public static void main(String args[])
Scanner sc = new Scanner(;
System.out.print("Enter a number to check: ");
//reading a number from the user
int N=sc.nextInt();
//calling user-defined function
//function checks whether the given is a perfect square or not
static boolean findPerfectSquare(double num)
//finds the square root of the ggiven number
double square_root = Math.sqrt(num);
//if square root is an integer
return((square_root - Math.floor(square_root)) == 0);
//function that checks whether the given number is Sunny or not
static void isSunnyNumber(int N)
//checks N+1 is perfect square or not
if (findPerfectSquare(N + 1))
System.out.println("The given number is a sunny number.");
//executes if N+1 is not a perfect square
System.out.println("The given number is not a sunny number.");


Enter a number to check: 8
The given number is a sunny number.

Applications of Sunny Numbers in Java

Sunny numbers have many uses in various disciplines, including computer science and mathematics, and are not just fascinating from a mathematical perspective.

  • Mathematics : In order to study perfect and amicable numbers, sunny numbers are used. There are still new insights being made into the relationship between harmonious, perfect, and sunny numbers.
  • Computer Science : Certain algorithms, particularly those in the field of cryptography, are designed using sunny numbers. Pseudorandom number generators are created using them. In computer graphics and image processing, in particular, they are employed in the creation of effective search algorithms.
  • Real-World Applications : Sunny numbers are used in meteorology to foretell the start of specific weather patterns, such as heat waves or cold snaps. Sunny numbers are used in economics to analyze economic cycles and forecast market trends. Sunny numbers are used in sports to evaluate player performance and forecast game results.

Overall, the idea of sunny numbers is intriguing and practical, and it has been used in a variety of academic disciplines. Sunny numbers are likely to have even more uses as research advances, becoming a crucial resource for mathematicians, engineers, and scientists alike.

The information in this article should have helped you learn more about “Sunny Number in Java ” and other crucial details. Keep checking back for more news from Java programming blogs. For more details on various programs like Core Python, DJANGO, HTML and more. 

About The Author

Leave a Reply