search
HomeJavajavaTutorialHow to check if a number is a sunshine number in Java?

How to check if a number is a sunshine number in Java?

If the square root of the next value of the input number is the perfect square of any number, the number is called a sunshine number.

To further explain, if we add 1 to any number, we get the next value. Then we need to find its square root. If we get an integer value, then we can say that it is the perfect square of some number. If we confirm that the next number has a perfect square, then the entered number is a sunshine number, otherwise it is not a sunshine number.

In this article, we will see how to check if a number is a sunshine number using Java programming language.

Show you some examples

The Chinese translation of

Instance-1

is:

Instance-1

The number entered is 80.

Let’s check this using the logic of sunshine numbers.

The next value of

80 = 80 1 = 81

The square root of 81=9

As we noticed here, 81 is the perfect square of 9.

Therefore, 80 is a sunny number.

The Chinese translation of

Instance-2

is:

Instance-2

The number entered is 48.

Let’s check this using the logic of sunshine numbers.

Next value of

48 = 48 1 = 49

The square root of 49=7

As we noticed here, 49 is a perfect square of 7.

Therefore, 48 is a sunshine number.

The Chinese translation of

Instance-3

is:

Instance-3

The number entered is 122.

Let’s check this using the logic of sunshine numbers.

Next value of

122 = 122 1 = 123

The square root of 123=11.09053651

As we noticed here, 123 is not a perfect square.

Therefore, 122 is a sunshine number.

Some other examples of sunshine numbers include 3, 8, 15, 24, 35, 48, 63, etc.

grammar

To get the square root of a number, we can use the built-in sqrt() method in the Math class in the java.lang package.

The following is the syntax for using this method to get the square root of any number.

double squareRoot = Math.sqrt(input_vale)

You can use Math.floor() to find the closest integer value.

Math.floor(square_root)

algorithm

  • Step 1 - Get an integer through initialization or user input.

  • Step 2 - Then we find its next value by adding 1 to it and store it in another variable.

  • Step 3 - We find the square root of the next value.

  • Step 4 - Now we are finding the closest perfect square root and subtracting it from the next square root value.

  • Step 5 - If the value after subtraction is zero, then we will get confirmation that it is an integer value, meaning that the next value is the perfect square of any number.

  • Step 6 − If we get confirmation that the next number is a perfect square number, then print that number is a sunshine number, otherwise it is not a sunshine number.

Multiple methods

We provide solutions in different ways.

  • By using static input values

  • By using user-defined methods

Let’s look at the program and its output one by one.

Method 1: By using static input values

In this method, the program will initialize an integer value, and then by using an algorithm, we can check whether a number is a sunshine number.

The Chinese translation of

Example

is:

Example

import java.util.*;
public class Main{
   public static void main(String args[]){
         //declare an int variable and initialize with a static value
         int inputNumber=8;

         //declare a variable which store next value of input number
         double next=inputNumber + 1;

         //Find the square root of the next number
         //store it as double value
         double square_root = Math.sqrt(next);

         //check whether the square root is a integer value or not
         //if yes return true otherwise false
         if(((square_root - Math.floor(square_root)) == 0))

         //if true then print it is a sunny number
            System.out.println(inputNumber + " is a sunny number.");
         else
            //if true then print it is a sunny number
            System.out.println(inputNumber + " is not a sunny number.");
   }
}

Output

8 is not a sunny number.

Method 2: By using user-defined methods

In this method we assign a static value as the input number and pass that number as parameter to a user defined method and then inside the method by using an algorithm we can check if the number is a Sunny numbers.

The Chinese translation of

Example

is:

Example

import java.util.*;
public class Main{
   public static void main(String args[]){
   //declare an int variable and initialize with a static value
   int inp=15;

   //call the user defined method inside the conditional statement
   if(checkSunny(inp))

   //if true then print it is a sunny number
      System.out.println(inp + " is a sunny number.");

   else
      //if true then print it is a sunny number
      System.out.println(" is not a sunny number.");
   }

   //define the user defined method
   static boolean checkSunny(int inputNumber){

      //declare a variable which store next value of input number
      double next=inputNumber + 1;

      //Find the square root of the next number
      // store it as double value
      double square_root = Math.sqrt(next);

      //check whether the square root is a integer value or not
      //if yes return true otherwise false
      return ((square_root - Math.floor(square_root)) == 0);
   }
}

Output

15 is a sunny number.

In this article, we explored how to check if a number is positive in Java using three different methods.

The above is the detailed content of How to check if a number is a sunshine number in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:tutorialspoint. If there is any infringement, please contact admin@php.cn delete
JVM performance vs other languagesJVM performance vs other languagesMay 14, 2025 am 12:16 AM

JVM'sperformanceiscompetitivewithotherruntimes,offeringabalanceofspeed,safety,andproductivity.1)JVMusesJITcompilationfordynamicoptimizations.2)C offersnativeperformancebutlacksJVM'ssafetyfeatures.3)Pythonisslowerbuteasiertouse.4)JavaScript'sJITisles

Java Platform Independence: Examples of useJava Platform Independence: Examples of useMay 14, 2025 am 12:14 AM

JavaachievesplatformindependencethroughtheJavaVirtualMachine(JVM),allowingcodetorunonanyplatformwithaJVM.1)Codeiscompiledintobytecode,notmachine-specificcode.2)BytecodeisinterpretedbytheJVM,enablingcross-platformexecution.3)Developersshouldtestacross

JVM Architecture: A Deep Dive into the Java Virtual MachineJVM Architecture: A Deep Dive into the Java Virtual MachineMay 14, 2025 am 12:12 AM

TheJVMisanabstractcomputingmachinecrucialforrunningJavaprogramsduetoitsplatform-independentarchitecture.Itincludes:1)ClassLoaderforloadingclasses,2)RuntimeDataAreafordatastorage,3)ExecutionEnginewithInterpreter,JITCompiler,andGarbageCollectorforbytec

JVM: Is JVM related to the OS?JVM: Is JVM related to the OS?May 14, 2025 am 12:11 AM

JVMhasacloserelationshipwiththeOSasittranslatesJavabytecodeintomachine-specificinstructions,managesmemory,andhandlesgarbagecollection.ThisrelationshipallowsJavatorunonvariousOSenvironments,butitalsopresentschallengeslikedifferentJVMbehaviorsandOS-spe

Java: Write Once, Run Anywhere (WORA) - A Deep Dive into Platform IndependenceJava: Write Once, Run Anywhere (WORA) - A Deep Dive into Platform IndependenceMay 14, 2025 am 12:05 AM

Java implementation "write once, run everywhere" is compiled into bytecode and run on a Java virtual machine (JVM). 1) Write Java code and compile it into bytecode. 2) Bytecode runs on any platform with JVM installed. 3) Use Java native interface (JNI) to handle platform-specific functions. Despite challenges such as JVM consistency and the use of platform-specific libraries, WORA greatly improves development efficiency and deployment flexibility.

Java Platform Independence: Compatibility with different OSJava Platform Independence: Compatibility with different OSMay 13, 2025 am 12:11 AM

JavaachievesplatformindependencethroughtheJavaVirtualMachine(JVM),allowingcodetorunondifferentoperatingsystemswithoutmodification.TheJVMcompilesJavacodeintoplatform-independentbytecode,whichittheninterpretsandexecutesonthespecificOS,abstractingawayOS

What features make java still powerfulWhat features make java still powerfulMay 13, 2025 am 12:05 AM

Javaispowerfulduetoitsplatformindependence,object-orientednature,richstandardlibrary,performancecapabilities,andstrongsecurityfeatures.1)PlatformindependenceallowsapplicationstorunonanydevicesupportingJava.2)Object-orientedprogrammingpromotesmodulara

Top Java Features: A Comprehensive Guide for DevelopersTop Java Features: A Comprehensive Guide for DevelopersMay 13, 2025 am 12:04 AM

The top Java functions include: 1) object-oriented programming, supporting polymorphism, improving code flexibility and maintainability; 2) exception handling mechanism, improving code robustness through try-catch-finally blocks; 3) garbage collection, simplifying memory management; 4) generics, enhancing type safety; 5) ambda expressions and functional programming to make the code more concise and expressive; 6) rich standard libraries, providing optimized data structures and algorithms.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools