Table of Contents
Show you some examples
Instance-3
grammar
algorithm
Multiple methods
Method 1: By using static input values
Example
Output
Method 2: By using user-defined methods
Home Java javaTutorial How to check if a number is a sunshine number in Java?

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

Aug 19, 2023 am 10:57 AM
java number examine Number of sunshine

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)
Copy after login

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

Math.floor(square_root)
Copy after login

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.");
   }
}
Copy after login

Output

8 is not a sunny number.
Copy after login

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);
   }
}
Copy after login

Output

15 is a sunny number.
Copy after login

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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 Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

How to Run Your First Spring Boot Application in Spring Tool Suite? How to Run Your First Spring Boot Application in Spring Tool Suite? Feb 07, 2025 pm 12:11 PM

Spring Boot simplifies the creation of robust, scalable, and production-ready Java applications, revolutionizing Java development. Its "convention over configuration" approach, inherent to the Spring ecosystem, minimizes manual setup, allo

What are the platforms for virtual currency trading around the world? The top ten latest 2025 digital currency app rankings What are the platforms for virtual currency trading around the world? The top ten latest 2025 digital currency app rankings Feb 27, 2025 pm 06:09 PM

The top four global virtual currency trading platforms in 2025 are: Binance: a leader in the industry, providing diversified trading options and innovative products. OKX: A huge user base, providing comprehensive cryptocurrency services. Gate.io: User-friendly, offering a wide range of cryptocurrency options. Bitget: Focus on derivatives trading and provides high leverage futures contracts.

Java Made Simple: A Beginner's Guide to Programming Power Java Made Simple: A Beginner's Guide to Programming Power Oct 11, 2024 pm 06:30 PM

Java Made Simple: A Beginner's Guide to Programming Power Introduction Java is a powerful programming language used in everything from mobile applications to enterprise-level systems. For beginners, Java's syntax is simple and easy to understand, making it an ideal choice for learning programming. Basic Syntax Java uses a class-based object-oriented programming paradigm. Classes are templates that organize related data and behavior together. Here is a simple Java class example: publicclassPerson{privateStringname;privateintage;

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

See all articles