- 1Java Interview Program Title
- 2Java Interview Program - Check Whether a Number is Armstrong
- 3Java Interview Program - Check Whether a Number is an Ugly Number
- 4Java Interview Program - Check Whether a Number is a Sunny Number in Java
- 5Java Interview Program - Check Whether a Number is a Harshad (Niven) Number
- 6Check whether a number is a Hamming number in Java
- 7Check Whether a Number is a Spy Number in Java
- 8Check whether a Number is a Smith Number in Java
- 9Check Whether a Number is a Disarium Number in Java
- 10Check Whether a Number is a Lychrel Number Candidate in Java
- 11Check Whether a Number is a Tech Number in Java
Java Interview Program - Check Whether a Number is a Sunny Number in Java
Check Whether a Number is a Sunny Number in Java
A sunny number is a number for which n + 1
is a perfect square. In simpler terms, if you add 1 to the number and the result is a perfect square, then the original number is considered a sunny number.
For example, 8 is a sunny number because 8 + 1 = 9, and 9 is a perfect square (3 × 3).
This program helps you check whether a given number is sunny or not by using mathematical operations and a built-in function to find the square root.
Examples
- Input: 8
Output: Sunny Number (since 8 + 1 = 9, and √9 = 3) - Input: 10
Output: Not a Sunny Number (since 10 + 1 = 11, and √11 is not an integer) - Input: 0
Output: Not a Sunny Number (since 0 + 1 = 1, and √1 = 1 → Sunny Number) - Input: 15
Output: Sunny Number (since 15 + 1 = 16, and √16 = 4)
Interviewer Expectations
Interviewers are testing:
- Your understanding of number properties and perfect squares
- Your ability to use Java’s
Math
library - Logical flow and condition checks
- Input validation and edge case handling
Approach
Here's how we can solve the problem step-by-step:
- Take an integer
n
as input. - Check whether
n + 1
is a perfect square. - To check if a number is a perfect square, calculate the square root and verify whether squaring it gives back the original number.
Dry Run Example
Input: 24
Step 1: 24 + 1 = 25
Step 2: √25 = 5
Step 3: 5 × 5 = 25 → matches
Output: Sunny Number
Java Program
public class SunnyNumberExample {
public static void main(String[] args) {
int n = 15;
int nextNum = n + 1;
double sqrt = Math.sqrt(nextNum);
if ((int)sqrt * (int)sqrt == nextNum) {
System.out.println(n + " is a Sunny Number.");
} else {
System.out.println(n + " is Not a Sunny Number.");
}
}
}
15 is a Sunny Number.
Possible Followup Questions with Answers
Q1: What is the difference between a Sunny Number and a Perfect Square?
A Perfect Square is a number that is the square of an integer (like 4, 9, 16). A Sunny Number is a number whose n + 1
is a perfect square. So a number itself doesn't have to be a perfect square to be sunny.
Q2: How do you check for a perfect square in Java?
int num = 25;
int sqrt = (int) Math.sqrt(num);
if (sqrt * sqrt == num) {
System.out.println("Perfect Square");
}
Q3: Can sunny numbers be negative?
No, since the square root of a negative number is not real in Java (it results in NaN), sunny number checks are only meaningful for non-negative integers.
Q4: How would you check multiple numbers in a loop?
for (int i = 1; i <= 20; i++) {
int next = i + 1;
int root = (int) Math.sqrt(next);
if (root * root == next) {
System.out.println(i + " is Sunny");
}
}
Next Topic ⮕Java Interview Program - Check Whether a Number is a Harshad (Niven) Number
Comments
Loading comments...