BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Sunny Number Program in Java

By Chaitanya Singh | Filed Under: Java Examples

In this tutorial, we will write a java program to check whether a given number is Sunny number or not. We will also write a program to find all the Sunny numbers in a given range.

Sunny Number

A number n is called Sunny number if the next number (n+1) is a perfect square number. For example 15 is a Sunny number because 15+1 =16 is a perfect square number.

Java Program to check Sunny Number

The logic that we are using here is:

  1. Take the input number from the user and store it in a variable num using Scanner class function nextInt()
  2. Find the square root of the next number (num+1).
  3. If the square root is an integer, it means that the input number is a Sunny number else it’s not a Sunny number.

In the following example, we have created two user-defined functions isSunny() and isPerfectSquare(). The function isSunny() checks whether the passed number is Sunny or not and the function isPerfectSquare() checks whether the passed number is perfect square or not.

import java.util.Scanner;
public class JavaExample
{
    public static void main(String args[])
    {
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter the number: ");

        //Storing the input number in a variable num
        int num = scan.nextInt();
        scan.close();

        /* Calling the user-defined function isSunny()
         * to check whether the input number is Sunny
         */
        isSunny(num);
    }

    /* This is the another function that we need to find whether
     * the passed number is perfect square or not.
     */
    static boolean isPerfectSquare(double num)
    {
        //Finds the square root of the passed number
        double square_root = Math.sqrt(num);

        //Whether the square root is an int or not.
        return((square_root - Math.floor(square_root)) == 0);
    }
    //This function checks whether the input number is Sunny
    static void isSunny(int num)
    {
        //we are checking whether the next number n+1 is perfect square
        if (isPerfectSquare(num + 1))
        {
            System.out.println(num + " is a Sunny number.");
        }
        //if not a perfect square that prints not a Sunny number
        else
        {
            System.out.println(num + " is not a Sunny number.");
        }
    }
}

Output 1: When user enters number 15

Enter the number: 15
15 is a Sunny number.

Output 2: When user enters number 5

Enter the number: 5
5 is not a Sunny number.

Explanation: As you can see that for the number 5, the program returned “not Sunny” thats because 5+1 = 6 is not a perfect square. On the other hand, in the output 1, program returned “is Sunny” because 15+1 =16 is perfect square of number 4.

Java Program to find all Sunny numbers in a given Range

Here we will write a java program to find all Sunny numbers in a given range (1 to 100).

import java.util.Scanner;
public class JavaExample
{
    //This function checks whether the passed number is Sunny or not
    public static boolean isSunnyNumber(int num)
    {
        /* Here we are finding the square root of num+1
         * and checking the remainder after dividing the
         * square root by 1, if the remainder is 0, it means
         * num+1 is perfect square and num is a Sunny number
         */
        if(Math.sqrt(num+1)%1 == 0)
            return true;
        else
            return false;
    }
    public static void main(String args[])
    {
        int start, end;
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter Starting range: ");
        start = scan.nextInt();

        System.out.print("Enter Ending range: ");
        end = scan.nextInt();

        System.out.println("All Sunny numbers from "+ start + " to "+ end+" are: ");
        for(int i=start; i<=end; i++)
        {
            /* Here we are checking all the numbers in the given range
             * if the isSunnyNumber() returns true then we are printing
             * the number else we are ignoring it.
             */
            if(isSunnyNumber(i))
                System.out.print(i +" ");
        }
    }
}

Output: When user enters 1 as starting range and 100 as ending range.

Enter Starting range: 1
Enter Ending range: 100
All Sunny numbers from 1 to 100 are: 
3 8 15 24 35 48 63 80 99 

To learn Java from Scratch, refer my Java tutorial for beginners.

Related Java Examples

  • Java program to calculate area of Square
  • Java Program to Find square root of a Number without sqrt
  • Java Program to display Armstrong numbers in a given range
  • Peterson Number Program in Java

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Programs

  • C Programs
  • Java Programs
  • C++ Programs

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap