# Sunny Number in Java

Previously we have developed many Java programs on numbers. In this post, we will develop a Java program to check whether the given number is a sunny number or not? We will also develop a Java program to find all sunny numbers in a given range or interval.

A number `N` is called sunny number if the square root of the number `N+1` is an integer number. Example:- 24 is a sunny number because 24+1 = 25 has a square root of 5 which is an integer.

Procedure to develop method to check the given number is sunny number or not,

1) Take a number
2) Add 1 in the given number
3) Find its square root value
4) If the square root is an integer then the given number is a sunny number else the given number is not a sunny number.

If a number is completely divisible by 1 then the number is integer number, but if it leaves some remainder then it is not an integer number.

``````import java.util.Scanner;

public class SunnyNumber {

// Method to check the Sunny number
public static boolean isSunny(int number) {
if(Math.sqrt(number+1)%1 == 0)
return true;
else return false;
}

// main method
public static void main(String[] args) {

// declare variables
int number = 0;
boolean result = false;

//create Scanner class object to take input
Scanner scan = new Scanner(System.in);

// take input from end-user
System.out.print("Enter an integer number::");
number = scan.nextInt();

// check number is Sunny number or not
result = isSunny(number);

// display result
if(result)
System.out.println(number +
" is a Sunny number.");
else
System.out.println(number +
" is not a Sunny number.");

// close Scanner class object
scan.close();
}
}``````

The output of the different test-cases:-

Enter an integer number:: 24
24 is a Sunny number.

Enter an integer number:: 20
20 is not a Sunny number.

## Java program to find all Sunny number in a given range

We can also find all Sunny numbers in between two given numbers. For this purpose, we need to take the help of a loop. Below program find all sunny number which exists in the given interval.

``````import java.util.Scanner;

public class SunnyNumberInRange {

// Method to check the Sunny number
public static boolean isSunny(int number) {
if(Math.sqrt(number+1)%1 == 0)
return true;
else return false;
}

// main method
public static void main(String[] args) {
// declare variables
int minRange = 0, maxRange = 0;

//create Scanner class object to take input
Scanner scan = new Scanner(System.in);

System.out.print("Enter minimum value of range:");
minRange = scan.nextInt();
System.out.print("Enter maximum value of range:");
maxRange = scan.nextInt();

// loop
System.out.println("The Sunny number from "+
minRange + " to "+ maxRange+" are: ");

for(int i=minRange; i<=maxRange; i++) {
// check number
if(isSunny(i))
System.out.print(i +" ");
}

// close Scanner class object
scan.close();
}
}``````

Output:-

Enter `minimum` value of range:1
Enter `maximum` value of range:100
The Sunny number from 1 to 100 are:
`3 8 15 24 35 48 63 80 99`

Enter `minimum` value of range:100
Enter `maximum` value of range:200
The Sunny number from 100 to 200 are:
`120 143 168 195`

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!