Sunny Number in JAVA
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 Sunny Number
Input :12
Output : It is not a Sunny Number
Input : 24
Output : It is a Sunny 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
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
Program Video : Watch Now
All the Best :)
Keep Learning :)
Comments
Post a Comment