DEV Community

Vidya
Vidya

Posted on

Strong,Perfect,Neon Number Programs

Strong Number
A Strong Number is a number in which the sum of the factorials of its digits is equal to the number itself.

Take each digit → find factorial → add them → if result = original number, it is a Strong Number.
Enter fullscreen mode Exit fullscreen mode

Example

 Digits: 1, 4, 5

1! = 1  
4! = 24  
5! = 120  

Sum = 1 + 24 + 120 = 145
Enter fullscreen mode Exit fullscreen mode

java

public class Strong {

    public static void main(String[] args) {
        int no=145;
        if(num(no)) {
            System.out.println(no +"is strong number");
        }
        else {
            System.out.println(no +" is not strong number");
        }
        }
    static boolean num(int no) {
        int temp=no;
         int sum=0;
        while(no>0) {
            int digit=no%10;

        int i=1;
        int fact=1;
        while(i<=digit) {
            fact*=i;
            i++;
        }
        sum+=fact;
        no=no/10;
    }
        return sum==temp;

}}

Enter fullscreen mode Exit fullscreen mode

output

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

Add all divisors (except the number) → if result = original number → it's a Perfect Number.

Enter fullscreen mode Exit fullscreen mode

Example

 Divisors of 6: 1, 2, 3

Sum = 1 + 2 + 3 = 6
Enter fullscreen mode Exit fullscreen mode

java

public class Perfect {

    public static void main(String[] args) {
        int no=28;
        if(num(no)) {
           System.out.println(no + "is perfect");
        }else {
            System.out.println(no +" is not perfect");
        }

    }
      static boolean num(int no) {
          int div=1;
          int sum=0;
          while(div<=no/2) {
              if(no%div==0) {
                  sum+=div;

              }
              div+=1;

              }
              return sum==no;
          }

      }
Enter fullscreen mode Exit fullscreen mode

output

Neon Number
A Neon Number is a number where the sum of the digits of its square is equal to the original number.

Square the number → add digits → if result = original number → it's a Neon Number.
Enter fullscreen mode Exit fullscreen mode

Example

9² = 81

Digits: 8 + 1 = 9
Enter fullscreen mode Exit fullscreen mode

Java

public class Neon {

    public static void main(String[] args) {
        int no=9;
        if(num(no)) {
            System.out.println(no +"is neon number");

    }
        else {
            System.out.println(no +" is not neon number");
        }
    }
    static boolean num(int no) {
        int div=no*no;
        int sum=0;
        while(div>0) {
         sum+=div%10;
         div=div/10;
    }

        return sum==no;
       }

    }

Enter fullscreen mode Exit fullscreen mode

output

Top comments (0)