Java program to check if a number is a sunny number or not:
In this post, we will learn how to check if a number is a sunny number or not. We will write one program that will take one number as input from the user and print one message if that number is a sunny number or not.
We will also write one program to check for sunny numbers in a range.
Before we start writing the program, let’s learn what is a sunny number and the algorithm to use to check if a number is a sunny number or not.
What is a sunny number:
A number n is called a sunny number if the value of n + 1 is a perfect square.
For example, 63 is a sunny number because 63 + 1 = 64 is a perfect square of 8. But, 62 is not a sunny number, because 62 + 1 = 63 not a perfect square of any number.
We will learn three different ways to check if a number is sunny or not.
Algorithm to check for a sunny number:
We will use the below algorithm to check if a number is sunny or not:
- Take one number as input from the user.
- Add 1 to the number and check if it is a perfect square or not.
- If yes, print that it is a sunny number. Else, print that it is not a sunny number.
Method 1: By using Math.sqrt and Math.floor:
Math.sqrt method returns a double value. Math.floor return the largest double value that is less than or equal to the argument. So, if the difference of Math.sqrt and Math.floor of the result of Math.sqrt is 0, it is a perfect squre.
For example,
public class Main {
public static void main(String[] args) {
double sqrt = Math.sqrt(123);
double floor = Math.floor(sqrt);
System.out.println("sqrt "+sqrt+" floor "+floor);
System.out.println(sqrt - floor);
}
}
It will print:
sqrt 11.090536506409418 floor 11.0
0.09053650640941768
As you can see, the result is not 0, because 123 is not a perfect square. For a perfect square number, it will be zero.
So, we can use this method to check if a number is perfect square or not. Let’s use it to check if a number is a sunny number or not:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int no;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number: ");
no = sc.nextInt();
double sqrt = Math.sqrt(no + 1);
double floor = Math.floor(sqrt);
if (sqrt - floor == 0) {
System.out.println("It is a sunny number");
} else {
System.out.println("It is not a sunny number");
}
}
}
If you run this, it will print output as like below:
Enter a number:
25
It is not a sunny number
Enter a number:
48
It is a sunny number
We are reading the user input number and storing it in no. It checks if no + 1 is a perfect square or not. If yes, it is a sunny number, else not.
Method 2: By using Math.sqrt and % :
We can also use modulo or % with Math.sqrt to check if a number is perfect square or not. The modulo operator returns the remainder by dividing a number by a different number. a % b will return the remainder if we divide a by b. So, if we use Math.sqrt(n) % 1, it will be always 0 if n is a perfect square.
We can use this method to check for a sunny number.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int no;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number: ");
no = sc.nextInt();
double sqrt = Math.sqrt(no + 1);
if (sqrt % 1 == 0) {
System.out.println("It is a sunny number");
} else {
System.out.println("It is not a sunny number");
}
}
}
It will print output as like below:
Enter a number:
24
It is a sunny number
Method 3: By converting the Math.sqrt value to int:
Another way to find a number is perfect square or not is by converting the result of Math.sqrt to integer. We can convert the result integer and we can compare its square with number + 1.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int no;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number: ");
no = sc.nextInt();
double sqrt = Math.sqrt(no + 1);
int intSqrt = (int) sqrt;
if (intSqrt * intSqrt == no + 1) {
System.out.println("It is a sunny number");
} else {
System.out.println("It is not a sunny number");
}
}
}
The square root value is converted to an integer, i.e. it will get the part before decimal and if it is a perfect square, no + 1 will be equal to square of intSqrt.
You will get same output.
Method 4: By using a different method:
We can create a new method and use any of these three algorithms discussed above. This method can be called from any other places and it will return the same output.
import java.util.Scanner;
public class Main {
public static boolean isSunny(int n) {
return Math.sqrt(n + 1) % 1 == 0;
}
public static void main(String[] args) {
int no;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number: ");
no = sc.nextInt();
if (isSunny(no)) {
System.out.println("It is a sunny number");
} else {
System.out.println("It is not a sunny number");
}
}
}
Here,
- isSunny is a new method that takes one integer and returns one boolean value.
- It uses Math.sqrt and % to check if a number is sunny or not.
- We are calling this method from main and based on its result, we are printing the message.
It will print similar output.
Printing sunny numbers in a range:
Let’s write a program to print all sunny numbers between 1 to 100. We will use a loop and call a method to check if a number is sunny or not. We can use a for loop or while loop.
The loop will run from 1 to 100 and inside each iteration, it will check if that value is sunny number or not.
If it is a sunny number, it will print that.
public class Main {
public static boolean isSunny(int n) {
return Math.sqrt(n + 1) % 1 == 0;
}
public static void main(String[] args) {
for(int i = 1; i<= 100; i++){
if(isSunny(i)){
System.out.print(i+" ");
}
}
}
}
I am using a for loop in this program. But you can also use any other loop as well. It will print all sunny numbers from 1 to 100.
It will print the below output:
3 8 15 24 35 48 63 80 99
These are the sunny numbers in between 1 to 100.
You might also like:
- 3 different Java programs to print Pascal’s triangle for a given height
- Java program to check if a number is a spy number or not
- Java program to check if a number is a duck number or not
- Java program to check if a number is evil number or not
- Java Math.random() method example to create random numbers
- Java program to check if a number is a spy number or not