Sunny Number in Java

In this section, we will explore sunny numbers and develop a Java program to identify them. Additionally, we will create a Java program to determine all the sunny numbers within a given range.

What is Sunny Number?

A sunny number is defined as a number where the next consecutive number is a perfect square. In simpler terms, if a number N satisfies the condition N+1 = K^2, where K is a perfect square, then N is considered a sunny number.

Sunny Number Example

Let’s consider the number 80 and determine if it is a sunny number or not.

For N = 80, adding 1 to N gives us 80 + 1 = 81. We observe that 81 is a perfect square since it can be expressed as 9^2. Therefore, 80 is indeed a sunny number.

Now, let’s examine the number 10.

For N = 10, adding 1 to N yields 10 + 1 = 11. We can see that 11 is not a perfect square since it cannot be expressed as the square of any whole number. Consequently, 10 is not a sunny number.

Steps to Find Sunny Number

Determining whether a number is a sunny number involves simple logic. Here is how it can be done:

  1. Read or initialize a number (num).
  2. Add 1 to the given number, i.e., num + 1.
  3. Calculate the square root of num + 1.
  4. Check if the square root is an integer.
  5. If the square root is an integer, then the given number is a sunny number. Otherwise, it is not a sunny number.

Sunny Number Java Program

SunnyNumberExample1.java

import java.util.*;   
public class SunnyNumberExample  
{   
//driver code   
public static void main(String args[])   
{   
Scanner sc = new Scanner(System.in);  
System.out.print("Enter a number to check: ");  
//reading a number from the user  
int N=sc.nextInt();  
//calling user-defined function   
isSunnyNumber(N);   
}       
//function checks whether the given is a perfect square or not   
static boolean findPerfectSquare(double num)   
{   
//finds the square root of the ggiven number  
double square_root = Math.sqrt(num);   
//if square root is an integer   
return((square_root - Math.floor(square_root)) == 0);   
}   
//function that checks whether the given number is Sunny or not  
static void isSunnyNumber(int N)   
{   
//checks N+1 is perfect square or not  
if (findPerfectSquare(N + 1))   
{   
System.out.println("The given number is a sunny number.");   
}   
//executes if N+1 is not a perfect square  
else  
{   
System.out.println("The given number is not a sunny number.");   
}   
}   
}

Output 1:

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.

SunnyNumberExample2.java

import java.util.Scanner;  
public class SunnyNumberExample1  
{  
//user-defined function that determines all the sunny numbers  
public static boolean isSunnyNumber(int number)   
{  
//determines the square root of number+1 using Math.sqrt() method  
if(Math.sqrt(number+1)%1 == 0)  
return true;  
else   
return false;  
}  
//driver code  
public static void main(String args[])   
{  
//lr is lower range and ur is upper range  
int lr = 0, ur = 0;  
Scanner scan = new Scanner(System.in);  
System.out.print("Enter lower range: ");  
//reads an integer (lr) from the user  
lr = scan.nextInt();  
System.out.print("Enter upper range: ");  
//reads an integer (ur) from the user  
ur = scan.nextInt();  
System.out.println("The Sunny number from "+ lr + " to "+ ur+" are: ");  
for(int i=lr; i<=ur; i++)   
{  
//calling user-defined number  
if(isSunny(i))  
System.out.print(i +" ");  
}  
}  
}

Output:

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

To learn more like Sunny Number in Java, follow tutorials.freshersnow.com.