Java Program to Check Sunny Number

A Sunny Number is a number whose next number is a perfect square. In other words, a number n is called a Sunny Number if n + 1 is a perfect square.

For example, 8 is a Sunny Number because 8 + 1 = 9, and 9 is a perfect square (3 × 3). Similarly, 15 is a Sunny Number because 15 + 1 = 16 (4 × 4).

1. Understanding the Problem

The objective is to determine whether a given number is a Sunny Number.

To check a Sunny Number, first add 1 to the given number and then determine whether the resulting value is a perfect square.

Number: 8
8 + 1 = 9 → Perfect Square → Sunny Number

Number: 15
15 + 1 = 16 → Perfect Square → Sunny Number

Number: 10
10 + 1 = 11 → Not a Perfect Square → Not a Sunny Number

2. Java Program Using Basic Loop

A loop can be used to check whether n + 1 is a perfect square. The program tests possible square values until their square becomes greater than the target number.

Java
Check Sunny Number using a basic loop
import java.util.Scanner;

public class SunnyNumber {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int num = sc.nextInt();

        int next = num + 1;
        boolean isSunny = false;

        for (int i = 1; i * i <= next; i++) {
            if (i * i == next) {
                isSunny = true;
                break;
            }
        }

        if (isSunny)
            System.out.println(num + " is a Sunny Number");
        else
            System.out.println(num + " is not a Sunny Number");

        sc.close();
    }
}

How It Works

If the input is 8, the program calculates 8 + 1 = 9. The loop checks 1 × 1, 2 × 2, and 3 × 3. Since 3 × 3 equals 9, the program identifies 8 as a Sunny Number.

The time complexity of this approach is O(√n) because the loop checks possible square roots up to the square root of n + 1.

3. Optimized Approach Using Math.sqrt()

Java provides the Math.sqrt() method, which can be used to find the square root of n + 1. After converting the result to an integer, we can multiply it by itself to verify whether the original value is a perfect square.

Java
Check Sunny Number using Math.sqrt()
import java.util.Scanner;

public class SunnyOptimized {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int num = sc.nextInt();

        int next = num + 1;
        int sqrt = (int) Math.sqrt(next);

        if (sqrt * sqrt == next)
            System.out.println(num + " is a Sunny Number");
        else
            System.out.println(num + " is not a Sunny Number");

        sc.close();
    }
}

Explanation

For an input of 15, the program calculates next = 15 + 1 = 16. Math.sqrt(16) returns 4.0, which is converted to 4. Since 4 × 4 = 16, the number 15 is a Sunny Number.

The square root approach is simpler and more efficient than checking every possible value with a loop.

The time complexity is O(1) for fixed-size integer input.

4. Java Program Using a Separate Method

For better code organization and reusability, the Sunny Number logic can be placed inside a separate method.

Java
Check Sunny Number using a reusable method
import java.util.Scanner;

public class SunnyNumber {

    static boolean isSunny(int num) {
        int next = num + 1;
        int sqrt = (int) Math.sqrt(next);

        return sqrt * sqrt == next;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int num = sc.nextInt();

        if (isSunny(num))
            System.out.println(num + " is a Sunny Number");
        else
            System.out.println(num + " is not a Sunny Number");

        sc.close();
    }
}

5. Sample Input and Output

Enter a number: 8
8 is a Sunny Number
Enter a number: 15
15 is a Sunny Number
Enter a number: 10
10 is not a Sunny Number

6. Examples of Sunny Numbers

Some Sunny Numbers are 0, 3, 8, 15, 24, 35, 48, 63, and 80.

0 + 1 = 1  = 1²
3 + 1 = 4  = 2²
8 + 1 = 9  = 3²
15 + 1 = 16 = 4²
24 + 1 = 25 = 5²
35 + 1 = 36 = 6²

In general, Sunny Numbers can be represented as n = k² - 1, where k is a positive integer.

7. Common Mistakes

1. Forgetting to add 1 to the given number. The condition must be based on n + 1.

2. Checking whether the original number is a perfect square instead of checking whether n + 1 is a perfect square.

3. Comparing floating-point square roots directly instead of verifying the square of the integer square root.

4. Forgetting that Math.sqrt() returns a double value.

5. Not closing the Scanner after reading the input.

8. Applications and Learning Benefits

Sunny Number programs are useful beginner-level programming exercises for practicing mathematical logic and Java fundamentals.

They help students understand loops, conditional statements, perfect-square checking, Math.sqrt(), type casting, Boolean variables, Scanner input, and reusable methods.

Conclusion

A Sunny Number is a number for which n + 1 is a perfect square. For example, 8 is a Sunny Number because 8 + 1 = 9 = 3².

The loop-based approach is useful for understanding the logic behind perfect-square checking, while the Math.sqrt() approach provides a shorter and more efficient solution.

For most Java programs, the Math.sqrt() approach is a simple and readable way to check whether a number is Sunny.