DEV Community

Divya Divya
Divya Divya

Posted on

Neon Number , Strong Number , Perfect Number

Neon

The sum of the digits of its square is equal to the original number

  • Take a Number

  • Find its Square

  • Add the digits of square

  • If result == Original Number (is a Neon)

Example:
Square of 9

  • 9 × 9 = 81
    Sum of digits

  • 8 + 1 = 9
    Compare

  • Result = 9 (same as original)

So, 9 is a Neon number

Python

def neon(no):
    square=no*no
    sum=0
    while square > 0:
        sum=sum+square%10
        square=square//10
    if sum==no:
        print("Neon")
    else:
        print("Not Neon")
neon(9)   
Enter fullscreen mode Exit fullscreen mode

JavaScript

function neon(no) {
    let square = no * no;
    let sum = 0;

    while (square > 0) {
        sum = sum + (square % 10);
        square = Math.floor(square / 10);
    }

    if (sum === no) {
        console.log("Neon");
    } else {
        console.log("Not Neon");
    }
}

neon(9);
Enter fullscreen mode Exit fullscreen mode

Java

class Main {
    public static void neon(int no) {
        int square = no * no;
        int sum = 0;

        while (square > 0) {
            sum = sum + (square % 10);
            square = square / 10;
        }

        if (sum == no) {
            System.out.println("Neon");
        } else {
            System.out.println("Not Neon");
        }
    }

    public static void main(String[] args) {
        neon(9);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Strong

A Strong Number is number where:
Sum of factorial of each digits == the number itself.

  • Take the number => 145

  • Separate each digit => 1,4,5

  • Find factorial of each digit => 1,24,120

  • Add all factorial => 1+24+120 =145

  • Result == Original number => 145==145.

Python

def strong(no):
    n=no
    result=0
    while n>0:
        result=result+fact(n%10)
        n=n//10
    if result == no:
        print("Strong Number")
    else:
        print("Not Strong Number")

def fact(no):
    if no == 1:
        return 1
    return no*fact(no-1)

strong(145)
Enter fullscreen mode Exit fullscreen mode

JavaScript

function strong(no) {
    let n = no;
    let result = 0;

    while (n > 0) {
        result = result + fact(n % 10);
        n = Math.floor(n / 10);
    }

    if (result === no) {
        console.log("Strong Number");
    } else {
        console.log("Not Strong Number");
    }
}

function fact(no) {
    if (no === 0) {
        return 1;
    }
    return no * fact(no - 1);
}

strong(145);

Enter fullscreen mode Exit fullscreen mode

Java

class Main {

    public static void strong(int no) {
        int n = no;
        int result = 0;

        while (n > 0) {
            result = result + fact(n % 10);
            n = n / 10;
        }

        if (result == no) {
            System.out.println("Strong Number");
        } else {
            System.out.println("Not Strong Number");
        }
    }

    public static int fact(int no) {
        if (no == 0) {
            return 1;
        }
        return no * fact(no - 1);
    }

    public static void main(String[] args) {
        strong(145);
    }
}

Enter fullscreen mode Exit fullscreen mode

Output

Perfect

A Perfect Number is a positive integer that is equal to the sum of its proper divisors (excluding the number itself).

Example:
6
Divisors → 1, 2, 3
Sum → 1 + 2 + 3 = 6

So, 6 is a Perfect Number

Python

def perfect(n):
    div=1
    divadd=0
    while div<=n/2:
       if n%div==0:
           divadd+=div
       div+=1
    if n== divadd:
         print("Perfect")
    else:
         print("Not Perfect")
perfect(6)

Enter fullscreen mode Exit fullscreen mode

JavaScript

function perfect(n) {
    let div = 1;
    let divadd = 0;

    while (div <= Math.floor(n / 2)) {
        if (n % div === 0) {
            divadd += div;
        }
        div++;
    }

    if (n === divadd) {
        console.log("Perfect");
    } else {
        console.log("Not Perfect");
    }
}

perfect(8);

Enter fullscreen mode Exit fullscreen mode

Java

class Main {

    public static void perfect(int n) {
        int div = 1;
        int divadd = 0;

        while (div <= n / 2) {
            if (n % div == 0) {
                divadd += div;
            }
            div++;
        }

        if (n == divadd) {
            System.out.println("Perfect");
        } else {
            System.out.println("Not Perfect");
        }
    }

    public static void main(String[] args) {
        perfect(8);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Top comments (0)