# How To Check Whether a Number is a Sunny Number or Not in Java?

A number is said to be a Sunny number, if the square root of the next value of input number is a perfect square of any number.

For more clarification, if we add one to any number we get the next value. After that we have to find out the square root of it. If we get any integer value then we can say that it is a perfect square of any number. If we confirm that the next number has a perfect square number then the input number is a sunny number otherwise it is not a sunny number.

In this article we will see how to check if a number is sunny number by using Java programming language.

## To show you some instances

### Instance-1

Input number is 80.

Let’s check it by using the logic of Sunny number.

The next value of 80 = 80 + 1 = 81

The square root of 81 = 9

As we notice here 81 has a perfect square of 9.

Hence, 80 is a Sunny number.

### Instance-2

Input number is 48.

Let’s check it by using the logic of Sunny number.

The next value of 48 = 48 + 1 = 49

The square root of 49 = 7

As we notice here 49 has a perfect square of 7.

Hence, 48 is a Sunny number.

### Instance-3

Input number is 122.

Let’s check it by using the logic of Sunny number.

The next value of 122 = 122 + 1 = 123

The square root of 123 = 11.09053651

As we notice here 123 has not a perfect square.

Hence, 122 is a Sunny number.

Some of other examples of sunny numbers include 3, 8, 15, 24, 35, 48, 63 etc.

## Syntax

To get the square root of a number we have inbuilt sqrt() method in the Math class of java.lang package.

Following is the syntax to get square root of any number by using the method.

double squareRoot = Math.sqrt(input_vale)


You can use Math.floor() to find the nearest integer value.

Math.floor(square_root)


## Algorithm

• Step 1 − Get an integer number either by initialization or by user input.

• Step 2 − Then we find the next value of it by adding 1 to it and store it in another variable.

• Step 3 − We find the square root of the next value.

• Step 4 − Now we are finding the nearest perfect square root and subtract it with the square root value next.

• Step 5 − If after subtraction the value is zero then we will get the confirmation that, it is an integer value which indicate that the next value is a perfect square of any number.

• Step 6 − If we get the confirmation that the next number is a perfect square then print that the number is a sunny number otherwise it is not a sunny number.

## Multiple Approaches

We have provided the solution in different approaches.

• By Using Static Input Value

• By Using User Defined method

Let’s see the program along with its output one by one.

### Approach-1: By Using Static Input Value

In this approach one integer value will be initialized in the program and then by using the algorithm we can check whether a number is a sunny number or not.

#### Example

import java.util.*;
public class Main{
public static void main(String args[]){
//declare an int variable and initialize with a static value
int inputNumber=8;

//declare a variable which store next value of input number
double next=inputNumber + 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(inputNumber + " is a sunny number.");
else
//if true then print it is a sunny number
System.out.println(inputNumber + " is not a sunny number.");
}
}


#### Output

8 is not a sunny number.


### Approach-2: By Using User Defined Method

In this approach, We assign a static value as input number and pass this number as parameter in a user defined method then inside the method by using the algorithm we can check whether the number is a Sunny number or not.

#### Example

import java.util.*;
public class Main{
public static void main(String args[]){
//declare an int variable and initialize with a static value
int inp=15;

//call the user defined method inside the conditional statement
if(checkSunny(inp))

//if true then print it is a sunny number
System.out.println(inp + " is a sunny number.");

else
//if true then print it is a sunny number
System.out.println(" is not a sunny number.");
}

//define the user defined method
static boolean checkSunny(int inputNumber){

//declare a variable which store next value of input number
double next=inputNumber + 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
return ((square_root - Math.floor(square_root)) == 0);
}
}


#### Output

15 is a sunny number.


In this article, we explored how to check a number whether it is a sunny number or not in Java by using three different approaches.

Updated on: 28-Oct-2022

4K+ Views