Computer Science, asked by namanksavla, 8 months ago

Write a program to input a number and using ternary operator check whether it is a sunny number or not.

Answers

Answered by adityakumarjaiswal45
7

Answer:

Sunny Number in Java

  • Take a number.
  • Add 1 in the given number.
  • Find its square root value.
  • If the square root is an integer then the given number is a sunny number else the given number is not a sunny number.

Question

If a number is completely divisible by 1 then the number is integer number, but if it leaves some remainder then it is not an integer number.

Answere

import java.util.Scanner;

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();

}

}

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();

}

}

The output of the different test-cases:-

Enter an integer number:: 24

24 is a Sunny number.

Enter an integer number:: 20

20 is not a Sunny number.

I HOPE IT HELP YOU ✌️✌️✌️

THANKS YOU

Similar questions