In this tutorial, we will write a java program to check whether a given number is Sunny number or not. We will also write a program to find all the Sunny numbers in a given range.
Sunny Number
A number n is called Sunny number if the next number (n+1) is a perfect square number. For example 15 is a Sunny number because 15+1 =16 is a perfect square number.
Java Program to check Sunny Number
The logic that we are using here is:
- Take the input number from the user and store it in a variable num using Scanner class function nextInt()
- Find the square root of the next number (num+1).
- If the square root is an integer, it means that the input number is a Sunny number else it’s not a Sunny number.
In the following example, we have created two user-defined functions isSunny()
and isPerfectSquare()
. The function isSunny()
checks whether the passed number is Sunny or not and the function isPerfectSquare()
checks whether the passed number is perfect square or not.
import java.util.Scanner; public class JavaExample { public static void main(String args[]) { Scanner scan = new Scanner(System.in); System.out.print("Enter the number: "); //Storing the input number in a variable num int num = scan.nextInt(); scan.close(); /* Calling the user-defined function isSunny() * to check whether the input number is Sunny */ isSunny(num); } /* This is the another function that we need to find whether * the passed number is perfect square or not. */ static boolean isPerfectSquare(double num) { //Finds the square root of the passed number double square_root = Math.sqrt(num); //Whether the square root is an int or not. return((square_root - Math.floor(square_root)) == 0); } //This function checks whether the input number is Sunny static void isSunny(int num) { //we are checking whether the next number n+1 is perfect square if (isPerfectSquare(num + 1)) { System.out.println(num + " is a Sunny number."); } //if not a perfect square that prints not a Sunny number else { System.out.println(num + " is not a Sunny number."); } } }
Output 1: When user enters number 15
Enter the number: 15 15 is a Sunny number.
Output 2: When user enters number 5
Enter the number: 5 5 is not a Sunny number.
Explanation: As you can see that for the number 5, the program returned “not Sunny” thats because 5+1 = 6 is not a perfect square. On the other hand, in the output 1, program returned “is Sunny” because 15+1 =16 is perfect square of number 4.
Java Program to find all Sunny numbers in a given Range
Here we will write a java program to find all Sunny numbers in a given range (1 to 100).
import java.util.Scanner; public class JavaExample { //This function checks whether the passed number is Sunny or not public static boolean isSunnyNumber(int num) { /* Here we are finding the square root of num+1 * and checking the remainder after dividing the * square root by 1, if the remainder is 0, it means * num+1 is perfect square and num is a Sunny number */ if(Math.sqrt(num+1)%1 == 0) return true; else return false; } public static void main(String args[]) { int start, end; Scanner scan = new Scanner(System.in); System.out.print("Enter Starting range: "); start = scan.nextInt(); System.out.print("Enter Ending range: "); end = scan.nextInt(); System.out.println("All Sunny numbers from "+ start + " to "+ end+" are: "); for(int i=start; i<=end; i++) { /* Here we are checking all the numbers in the given range * if the isSunnyNumber() returns true then we are printing * the number else we are ignoring it. */ if(isSunnyNumber(i)) System.out.print(i +" "); } } }
Output: When user enters 1 as starting range and 100 as ending range.
Enter Starting range: 1 Enter Ending range: 100 All Sunny numbers from 1 to 100 are: 3 8 15 24 35 48 63 80 99
To learn Java from Scratch, refer my Java tutorial for beginners.
Leave a Reply