Sunny Number in Java
In this section, we will learn what is sunny numbers and how to create a Java program to find the sunny numbers. We will also create a Java program to find all the sunny numbers between the specified range.
A number is called a sunny number if the number next to the given number is a perfect square. In other words, a number N will be a sunny number if N+1 is a perfect square.
Let's understand it through an example.
Sunny Number Example
Suppose, we have to check if 80 is a sunny number or not.
Given, N=80 then N+1 will be 80+1=81, which is a perfect square of the number 9. Hence 80 is a sunny number.
Let's take another number 10.
Given, N=10 then N+1 will be 10+1=11, which is not a perfect square. Hence 10 is not a sunny number.
Steps to Find Sunny Number
The logic is very simple. To find the sunny number, we need only to check whether N+1 is the perfect square or not.
Let's implement the above steps in a Java program.
Sunny Number Java Program
Enter a number to check: 80 The given number is a sunny number.
Enter a number to check: 670 The given number is not a sunny number.
Let's create another Java program and find all the sunny numbers between a given range.
Enter lower range: 1 Enter upper range: 1000 The Sunny number from 1 to 1000 are: 3 8 15 24 35 48 63 80 99 120 143 168 195 224 255 288 323 360 399 440 483 528 575 624 675 728 783 840 899 960
Next TopicTech Number in Java