Kodekraftt
/Blog
/Sunny Number In Java Meaning Explanation Examples And Practice Problems
Join Early AccessContact UsPrivacy Policy
Java BasicsOOPsDSA with JavaQuizzesInterview Preparation

© 2026 KodeKraftt. All rights reserved.

Build smarter. Learn more. Innovate better.

Sunny Number in Java: Meaning, Explanation, Examples, and Practice Problems

Learn what a Sunny Number is in Java with definition, examples, logic, and a complete Java program. Understand how to check Sunny Numbers and practice with 3 coding challenges for students.

Sunny Number in Java: Meaning, Explanation, Examples, and Practice Problems

A Sunny Number is a number for which the next consecutive number is a perfect square. This is a well-known number theory concept often asked in coding interviews and beginner Java assignments due to its simple and logical approach.

What Is a Sunny Number?

A number N is a Sunny Number if:

N + 1 is a perfect square

That means if you add 1 to the number, the result must be a complete perfect square like 4, 9, 16, 25, and so on.

Examples of Sunny Numbers

  • 3 → 3 + 1 = 4 (perfect square)
  • 8 → 8 + 1 = 9 (perfect square)
  • 15 → 15 + 1 = 16 (perfect square)
  • 24 → 24 + 1 = 25 (perfect square)

How to Check Sunny Number?

Steps to determine if a number is Sunny:

  1. Add 1 to the given number.
  2. Find the square root of the new number.
  3. If the square root is an integer → it is a Sunny Number.
  4. If not → it is not a Sunny Number.

Java Program to Check Sunny Number


import java.util.*;

public class SunnyNumber {

    static boolean isPerfectSquare(int n) {
        int root = (int)Math.sqrt(n);
        return root * root == n;
    }

    public static void main(String[] args) {

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

        if (isPerfectSquare(num + 1)) {
            System.out.println("Sunny Number");
        } else {
            System.out.println("Not a Sunny Number");
        }
    }
}

  

Examples Table

Number N + 1 Perfect Square? Sunny?
3 4 Yes Sunny Number
8 9 Yes Sunny Number
14 15 No Not Sunny
24 25 Yes Sunny Number
30 31 No Not Sunny

3 Practice Challenges

Challenge 1: Print Sunny Numbers in a Range

Write a Java program to display all Sunny Numbers between 1 and 10,000.

Challenge 2: Sunny Numbers in an Array

Accept an array from the user and count how many elements are Sunny Numbers.

Challenge 3: Next Sunny Number

Write a Java program to find the next Sunny Number after a given integer.

You might also like

Odious Number in Java: Definition, Rules, Examples, and Practice Problems

Learn what an Odious Number is in Java and number theory. Understand its definition, binary representation rules, examples, and how to check Odious Numbers using Java. Includes example table and 3 practice challenges.

Evil Number in Java: Definition, Examples, and Practice Problems

Learn what an Evil Number is in Java and number theory. Understand its definition, binary representation rules, examples, and how to check Evil Numbers using Java. Includes example table and 3 practice challenges.

Pronic Number in Java: Definition, Explanation, Examples, and Practice Problems

Learn what a Pronic Number is in Java with clear explanations, examples, and a complete Java program. Understand how to identify Pronic Numbers and practice with 3 coding challenges.

Strong Number in Java

Learn what a Strong Number is, how it works, and how to write an efficient Java program to check if a number is strong. Includes explanation, examples, algorithm, and clean Java implementation.

Buzz Number in Java: Definition, Rules, Examples, and Practice Problems

Learn what a Buzz Number is in number theory and Java programming. Understand its rules, examples, and how to check Buzz Numbers using Java. Includes sample program, table of examples, and 3 practice challenges.

Smith Number in Java: Definition, Steps, Examples, and Practice Problems

Learn what a Smith Number is in Java. Understand its definition, rules, examples, and how to check a Smith Number using Java. Includes detailed explanation and 3 practice challenges for students.