Google News
logo
Java program to Sunny Number
A sunny number is a number where the square root of the number incremented by 1 is a whole number.

For example, 24 is a sunny number because the square root of 24 + 1 = 5 is a whole number.

Here is a Java program to check if a number is a sunny number or not :
Program :
import java.util.Scanner;

public class SunnyNumber {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int n = sc.nextInt();
        sc.close();

        double s = Math.sqrt(n + 1);
        if ((int)s == s) {
            System.out.println(n + " is a sunny number.");
        } else {
            System.out.println(n + " is not a sunny number.");
        }
    }
}
Output :
Enter a number: 24
24 is a sunny number.
Enter a number: 27
27 is not a sunny number.
The program prompts the user to enter a number and checks if the square root of the number incremented by 1 is a whole number.

If it is, then the number is a sunny number, otherwise, it is not.