Sunny number in java

Checking Sunny number 


                                    Steps to do this:-
1. take two integers i,n ( n is for user input number and i is for to store (n+1) value (i=n+1)

2. take a double variable named b (this is for calculating the square root)

3.now check this 
b=Math.sqrt(i) // in this steps we use the sqrt function to calculate the square root and the answer will be stored in b 

now if b can be double value(means it has a point value Ex:-3.56) or it has a int value (ex:-3)

now we have to check that if the value is only the integer then print this is a "sunny number"

4. check if((int)b==b) print "sunny number" else print "not a sunny number "
in this step we just convert the double value to integer by type-casting to check that is the sqrt is integer or not

5.save and run


The Source Code

import java.util.Scanner;
class sunny
{
    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        int i,n;// i is for to store n+1 and n is for user input number
        double b;
        System.out.println("enter the number");
        n=s.nextInt();
        i=n+1;
        b=Math.sqrt(i);
        if((int)b==b)
        {
            System.out.println("this is sunny number");
        }
        else{
            System.out.println("this is not a sunny number");
        }
    }
}



OUTPUT

Watch My Video

Comments

Popular Posts