
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How To Check Whether a Number is a Sunny Number or Not in Java?
What is Sunny Number?
A number is said to be a Sunny number if the square root of the next value of the given number is a perfect square of any number. For example, let's consider the number 3. If we take the number after 3, which is 4, and 2 is its perfect square root.
Here are some other examples of sunny numbers such as 3, 8, 15, 24, 35, 48, 63, etc.
Input & Output Scenarios
Following are some input and output scenarios that provide a better understanding of the sunny number:
Scenario 1
Suppose the given input number is 80:
Input: 80 Output: Yes Calculation: 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, the 80 is a Sunny number.
Scenario 2
Suppose the given number is 47:
Input: 48 Output: No Calculation: The next value of 47 = 47 + 1 = 48 The square root of 48 = 6.92
Since the 48 has a no perfect square, the 48 is a not a Sunny number.
Let's see the program along with its output one by one.
Example 1
The following program checks whether the number 8 is a sunny number by determining if the next number 9, is a perfect square of any other number:
public class checkSunny{ public static void main(String args[]){ int inputNumber = 8; System.out.println("The given number is: " + inputNumber); //immidiate next of the original number double next = inputNumber + 1; //find the square root of the next number double square_root = Math.sqrt(next); //check whether the square root is a integer value or not if(((square_root - Math.floor(square_root)) == 0)){ System.out.println("Yes! " + inputNumber + " is a sunny number."); } else{ System.out.println("No! " + inputNumber + " is not a sunny number."); } } }
Output
The above program produces the following output:
The given number is: 8 Yes! 8 is a sunny number.
Example 2
This is another example of how to check if a number is a sunny number. We define a method called checkSunnyNumber() that will check whether the number 14 is a sunny number by determining whether the next number, 15, is a perfect square of any integer:
public class checkSunny { //method to check for sunny number static boolean checkSunnyNumber(int inputNumber){ //get the next value of given number double next = inputNumber + 1; double square_root = Math.sqrt(next); //check whether the square root is a integer value or not return ((square_root - Math.floor(square_root)) == 0); } public static void main(String args[]){ int input_number = 14; System.out.println("The given number is: " + input_number); //calling the checkSunnyNumber() method to check the sunny number System.out.println("Is the number " + input_number + " is a sunny number? " + checkSunnyNumber(input_number)); } }
Output
Following is the output of the above program:
The given number is: 14 Is the number 14 is a sunny number? false