write a program in java to input a number and using ternary operator check whether it is a sunny number or not.
Answers
Answer:
import java.util.Scanner;
public class SunnyNumber {
// Method to check the Sunny number
public static boolean isSunny(int number) {
if(Math.sqrt(number+1)%1 == 0)
return true;
else return false;
}
// main method
public static void main(String[] args) {
// declare variables
int number = 0;
boolean result = false;
//create Scanner class object to take input
Scanner scan = new Scanner(System.in);
// take input from end-user
System.out.print("Enter an integer number::");
number = scan.nextInt();
// check number is Sunny number or not
result = isSunny(number);
// display result
if(result)
System.out.println(number +
" is a Sunny number.");
else
System.out.println(number +
" is not a Sunny number.");
// close Scanner class object
scan.close();
}
}