top of page
  • Writer's pictureSatyam Gupta

Sunny Number in JAVA

Updated: Mar 11, 2021

Sunny Number

Sunny Number : A number 'n' is said to be a Sunny Number if the square root of 'n+1' number is an integer.


Example : 8 is a Special number since '8+1' i.e. 9 has a square root 3 which is an integer.


Input     : 8

Output  : It is a Special Number


Input     :12

Output  : It is not a Special Number


Input     : 24

Output  : It is a Special Number

 

Algorithm and Program given below :-

Algorithm :-

  • Input a number

  • Using for loop and store square of each number with value 1 less than the original in a variable

  • If this value equal to the entered number

  • Display "It is Sunny Number"

  • Else " It is not a Sunny Number" 

 

Program :- 


import java.io.*; import java.util.*; class Sunny {     public static void main(String args[])     {             Scanner in = new Scanner(System.in);         int n,i,a,c=0;         System.out.println("Enter Number : ");         n= in.nextInt(); // Input Number         for(i=1;i<=n;i++)         {             a= (i*i)-1; //storing square of an integer with 1 less value             if(a==n)             {                 c=1; // storing 1 if the condition is satisfied             }         }         if(c==1)         {             System.out.println("It is a Sunny Number : "+n);         }         else         {             System.out.println("It is not a Sunny Number : "+n);         }     } // end of main method } // end of class 

 

Note : The program is written in the easiest way possible so try to understand it. If you face any problem in understanding this program than feel free to contact/comment.


Source Code : Click here

Program Video : Watch now





All the Best :)

Keep Learning :)

2,394 views0 comments

Recent Posts

See All
Post: Blog2_Post
bottom of page