Sunny Number

Java coding interview problem for Number Logic: Sunny Number.

Checking whether a number is a Sunny Number is one of the most interesting mathematical coding interview questions.

This problem helps interviewers evaluate your understanding of:

  • Mathematical concepts
  • Square numbers
  • Square root
  • Loops
  • Conditional statements
  • Problem-solving skills

Sunny Number problems are frequently asked in Java, Python, C, C++, and aptitude-based coding interviews.


What is a Sunny Number?

A Sunny Number is a number whose next number is a perfect square.

In simple words,

Number + 1

is a Perfect Square

Mathematical Definition

If

N

is a number,

then

N + 1

must be a Perfect Square.

If

√(N + 1)

is an integer,

then

N is a Sunny Number.

Relationship Between Sunny Number and Perfect Square

A Sunny Number is directly related to a Perfect Square.

If

N + 1

is one of these numbers

1

4

9

16

25

36

49

64

81

100

then

N

is a Sunny Number.


Example 1

Number

3

Add one

3 + 1

=

4

Square Root

√4

=

2

Since

2

is an integer,

3 is a Sunny Number.

Example 2

Number

8

Add one

8 + 1

=

9

Square Root

√9

=

3

Therefore

8 is a Sunny Number.

Example 3

Number

15

Add one

15 + 1

=

16

Square Root

√16

=

4

Therefore

15 is a Sunny Number.

Example 4

Number

24

Add one

24 + 1

=

25

Square Root

√25

=

5

Therefore

24 is a Sunny Number.

Example 5

Number

10

Add one

10 + 1

=

11

Square Root

√11

=

3.316...

Since the square root is not an integer,

10 is NOT a Sunny Number.

Some Sunny Numbers

Number Number + 1 Perfect Square? Sunny Number?
0 1 ✅ Yes ✅ Yes
3 4 ✅ Yes ✅ Yes
8 9 ✅ Yes ✅ Yes
15 16 ✅ Yes ✅ Yes
24 25 ✅ Yes ✅ Yes
35 36 ✅ Yes ✅ Yes
10 11 ❌ No ❌ No
18 19 ❌ No ❌ No

Real Interview Question

Write a Java program to check whether a given number is a Sunny Number.


Understanding the Logic

Suppose

Number = 24

First,

add one.

24 + 1

=

25

Now calculate the square root.

√25

=

5

Since the square root is an integer,

24 is a Sunny Number.

Visual Representation

Input

24

Processing

24

↓

Add 1

↓

25

↓

Square Root

↓

5

↓

Is Integer?

↓

Yes

↓

Sunny Number

Brute Force Approach

The simplest solution is:

  • Read the number.
  • Add one.
  • Find the square root.
  • Check whether the square root is an integer.
  • Print the result.

Algorithm

Step 1

Read the number.

Step 2

Add one.

value = number + 1;

Step 3

Calculate the square root.

sqrt = Math.sqrt(value);

Step 4

Check whether the square root is an integer.

sqrt == (int) sqrt

Step 5

If true,

Sunny Number

Otherwise,

Not a Sunny Number

Dry Run

Input

Number = 8

Add one

9

Square Root

3

Comparison

3 == 3

Output

Sunny Number

Another Dry Run

Input

Number = 10

Add one

11

Square Root

3.316...

Comparison

3.316 != 3

Output

Not a Sunny Number

Approach 1 — Using Math.sqrt()

This is the simplest and most commonly used interview solution.


Complete Java Program

public class SunnyNumber {

    public static void main(String[] args) {

        int number = 24;

        int value = number + 1;

        double squareRoot = Math.sqrt(value);

        if (squareRoot == (int) squareRoot) {

            System.out.println(number + " is a Sunny Number");

        } else {

            System.out.println(number + " is NOT a Sunny Number");

        }

    }

}

Output

24 is a Sunny Number

Step-by-Step Code Explanation

Step 1

Declare the number.

int number = 24;

Current value

24

Step 2

Add one.

int value = number + 1;

Result

25

Step 3

Calculate the square root.

double squareRoot = Math.sqrt(value);

Result

5.0

Step 4

Check whether it is an integer.

squareRoot == (int) squareRoot

Calculation

5.0 == 5

↓

true

Step 5

Print the result.

24 is a Sunny Number

Example Execution

Input

Number = 15

Processing

15 + 1 = 16

↓

√16 = 4

↓

Integer

Output

15 is a Sunny Number

Input

Number = 18

Processing

18 + 1 = 19

↓

√19 = 4.358...

Output

18 is NOT a Sunny Number

Why Does This Work?

A number is considered a Sunny Number only when its next consecutive number is a Perfect Square.

The algorithm:

  1. Adds 1 to the number.
  2. Calculates the square root.
  3. Checks whether the square root is a whole number.

If the square root has no fractional part, the number satisfies the definition of a Sunny Number.


Advantages of This Approach

  • Very easy to understand.
  • Uses Java's built-in Math.sqrt() method.
  • Short and readable code.
  • Constant extra memory.
  • Frequently asked in beginner interviews.

Drawbacks

Although this solution is straightforward, interviewers often ask follow-up questions such as:

  • Can you solve this without using Math.sqrt()?
  • Can you create a reusable isSunny() method?
  • Can you print all Sunny Numbers within a range?
  • What is the time complexity?
  • How is a Sunny Number different from a Perfect Square?

In Part 2, we'll cover:

  • Pure Arithmetic Solution (Without Math.sqrt())
  • Reusable isSunny() Method
  • Printing Sunny Numbers in a Range
  • Time and Space Complexity
  • Comparison of Approaches
  • Common Interview Mistakes
  • Interview Follow-up Questions
  • Related Coding Problems
  • Key Takeaways
  • Interview Tips

Approach 2 — Pure Arithmetic Solution (Without Using Math.sqrt())

Some interviewers may ask:

Can you check whether a number is a Sunny Number without using Math.sqrt()?

Yes.

Instead of using the built-in square root function, we can determine whether number + 1 is a perfect square by checking all possible integers.

Although this approach is slower, it demonstrates your understanding of loops and mathematical logic.


Java Program

public class SunnyNumberArithmetic {

    static boolean isSunny(int number) {

        int value = number + 1;

        int i = 1;

        while (i * i <= value) {

            if (i * i == value) {

                return true;

            }

            i++;

        }

        return false;

    }

    public static void main(String[] args) {

        int number = 24;

        if (isSunny(number)) {

            System.out.println(number + " is a Sunny Number");

        } else {

            System.out.println(number + " is NOT a Sunny Number");

        }

    }

}

Output

24 is a Sunny Number

Approach 3 — Reusable Method

Creating a reusable method improves:

  • Code readability
  • Reusability
  • Unit testing
  • Production-quality code

Java Program

public class SunnyNumberMethod {

    static boolean isSunny(int number) {

        int value = number + 1;

        double squareRoot = Math.sqrt(value);

        return squareRoot == (int) squareRoot;

    }

    public static void main(String[] args) {

        int number = 35;

        if (isSunny(number)) {

            System.out.println(number + " is a Sunny Number");

        } else {

            System.out.println(number + " is NOT a Sunny Number");

        }

    }

}

Output

35 is a Sunny Number

Approach 4 — Print Sunny Numbers in a Range

Interviewers sometimes ask:

Print all Sunny Numbers between 1 and N.

We can simply reuse the isSunny() method.


Java Program

public class SunnyNumbersRange {

    static boolean isSunny(int number) {

        int value = number + 1;

        double squareRoot = Math.sqrt(value);

        return squareRoot == (int) squareRoot;

    }

    public static void main(String[] args) {

        int limit = 100;

        System.out.println("Sunny Numbers:");

        for (int i = 0; i <= limit; i++) {

            if (isSunny(i)) {

                System.out.print(i + " ");

            }

        }

    }

}

Sample Output

Sunny Numbers:

0 3 8 15 24 35 48 63 80 99

Note: The output depends on the selected range.


Dry Run (Reusable Method)

Input

Number = 24

Processing

24

↓

Add 1

↓

25

↓

Square Root

↓

5

↓

Integer?

↓

Yes

↓

Return true

Time Complexity

Suppose

N

is the given number.


Using Math.sqrt()

Operation Complexity
Time O(1)
Space O(1)

The square root operation is treated as a constant-time library function.


Arithmetic Solution

Operation Complexity
Time O(√N)
Space O(1)

The loop checks values from 1 to √(N + 1).


Printing Sunny Numbers in a Range

Operation Complexity
Time O(n) using Math.sqrt()
Space O(1)

where

n

is the upper limit.


Comparison of Approaches

Approach Time Space Recommended
Using Math.sqrt() O(1) O(1) ✅ Best for Interviews
Pure Arithmetic O(√N) O(1) Good for Learning
Reusable Method O(1) O(1) Production Ready
Range Solution O(n) O(1) Useful Follow-up

Common Mistakes

Mistake 1

Checking the original number instead of adding one.

Wrong

Math.sqrt(number)

Correct

Math.sqrt(number + 1)

Mistake 2

Using floating-point comparison incorrectly.

Wrong

squareRoot == 5

Correct

squareRoot == (int) squareRoot

Mistake 3

Confusing Sunny Numbers with Perfect Squares.

Example

25

is a Perfect Square,

but

25 + 1 = 26

is not a Perfect Square.

Therefore,

25 is NOT a Sunny Number.

Mistake 4

Ignoring the number 0.

0 + 1 = 1

√1 = 1

Therefore,

0 is also a Sunny Number.

Mistake 5

Using unnecessary loops when Math.sqrt() is allowed.

In interviews, prefer the simpler solution unless the interviewer specifically asks for an arithmetic-only approach.


Interview Follow-up Questions

Q1. What is a Sunny Number?

Q2. Why is 24 a Sunny Number?

Q3. Is 25 a Sunny Number?

Q4. Can you solve this without using Math.sqrt()?

Q5. Print all Sunny Numbers in a range.

Q6. What is the time complexity?

Q7. Can negative numbers be Sunny Numbers?

Q8. Explain the relationship between Sunny Numbers and Perfect Squares.

Q9. Compare a Sunny Number and a Perfect Square.

Q10. Write a reusable isSunny() method.


Related Coding Problems

  • Perfect Square
  • Neon Number
  • Automorphic Number
  • Spy Number
  • Strong Number
  • Armstrong Number
  • Happy Number
  • Harshad Number

Key Takeaways

  • A Sunny Number is a number whose next consecutive number is a Perfect Square.
  • Common Sunny Numbers include:
0

3

8

15

24

35

48

63

80

99
  • Add one to the number:
value = number + 1;
  • Check whether the result is a Perfect Square.
  • The simplest solution uses:
Math.sqrt(value)
  • If the square root is an integer, the number is Sunny.
  • Using Math.sqrt() gives an O(1) solution, while the arithmetic-only approach runs in O(√N) time.

Frequently Asked Interview Questions

Q1. What is a Sunny Number?

A Sunny Number is a number whose next number is a Perfect Square.

Example

8 + 1 = 9

√9 = 3

Therefore,

8 is a Sunny Number.

Q2. Is 24 a Sunny Number?

Yes.

24 + 1 = 25

√25 = 5

Since 5 is an integer,

24 is a Sunny Number.

Q3. Is 25 a Sunny Number?

No.

25 + 1 = 26

√26 = 5.099...

The square root is not an integer.


Q4. Can negative numbers be Sunny Numbers?

Generally, No.

Sunny Numbers are typically defined for non-negative integers.


Q5. What is the difference between a Sunny Number and a Perfect Square?

Sunny Number Perfect Square
Number + 1 is a Perfect Square. The number itself is a square of an integer.
Example: 24 because 24 + 1 = 25. Example: 25 = 5 × 5.

Interview Tip

If an interviewer asks:

"Write a Java program to check whether a number is a Sunny Number."

Begin by explaining the mathematical definition: a number is Sunny if number + 1 is a Perfect Square. Implement the solution using Math.sqrt() because it is concise, efficient, and easy to understand. After computing number + 1, calculate its square root and verify that the result is an integer by comparing it with its integer cast. Mention that this approach runs in O(1) time with O(1) extra space. If the interviewer asks you to avoid library methods, explain the arithmetic approach that checks every integer from 1 to √(number + 1) until a matching square is found.