Java Program to Check Sunny Number

In the previous article, we have discussed Java Program to Check Perfect Number

In this article we are going to understand what Sunny number is and how we can check whether a number is Sunny number or not in Java with examples.

Program to Check Sunny Number

Sunny numbers are numbers whose successors are perfect square numbers i.e. the next number(n+1) is a perfect square.

Example :

15: 15+1= 16(Perfect Square) Sunny number
10: 10+1=11(Not a perfect square) Not a Sunny number
63= 63+1=64(Perfect Square) Sunny number

In the above examples the numbers 15 and 63 are Sunny numbers as their successors 16 and 64 are perfect square numbers. However 10 is not the Sunny number here as its successor 11 is not a perfect number.

Let’s see different ways to check sunny number.

Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.

Approach :

  1. Enter/declare a number and store it .
  2. We increment the number and then check if the incremented number is a perfect square or not.
  3. If the increment number is a perfect square, then the entered number is said to be a Sunny number.

Method-1: Java Program to Check Sunny Number By Using Static Value

import java.util.Scanner;
public class SunnyNumber{
    public static void main(String args[])
    {
        //A number declared;
        int num = 63;

        boolean flag = false;
        
        //Check if the number is a perfect square or not
        //isPerfectSquare() method called
        flag= isPerfectSquare(num);
        if(flag)
        {
            System.out.println(num+" is a Sunny Number");
        }
        else
        {
            System.out.println(num+" is Not a Sunny Number");
        }
    }

    // method to check for perfectsquare numbers
    static boolean isPerfectSquare(int num)
    {
        double root = Math.sqrt(++num);
        if((Math.floor(root) - root)==0)
            return true;
        else
            return false;
        
    }
}
Output:

63 is a Sunny Number

Method-2: Java Program to Check Sunny Number By User Input Value

import java.util.Scanner;
public class SunnyNumber{
    public static void main(String args[])
    {
        //Taking the number as input from the user using scanner class
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a number : ");
        int num = scan.nextInt();

        boolean flag = false;
        
        //Check if the number is a perfect square or not
        //isPerfectSquare() method called
        flag= isPerfectSquare(num);
        if(flag)
        {
            System.out.println(num+" is a Sunny Number");
        }
        else
        {
            System.out.println(num+" is Not a Sunny Number");
        }
    }

    // method to check for perfectsquare numbers
    static boolean isPerfectSquare(int num)
    {
        double root = Math.sqrt(++num);
        if((Math.floor(root) - root)==0)
            return true;
        else
            return false;
        
    }
}
Output:

Case-1

Enter a number : 15
15 is a Sunny Number

Case-2

Enter a number : 86
86 is not a Sunny Number

Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.

Related Java Programs: