Input a number to check for compositenumber or Niven number. Design a program to create a class perfect having two functions, one as composite(int a) another as Niven(int b)[A composite number has more than two factors]

[A Niven number is a number which is divisible by its sum of digits. For example 18 is a niven number because it is divisible by its sum of digits i.e9]

import java.util.*;
class perfect_Q2
{
    public static void composite(int a)
    {
        int count=0;
        for(int i=1;i<=a;i++)
        {
            if(a%i==0)
                count++;
        }
        if(count>2)
            System.out.println("It is a composite number");
        else
            System.out.println("It is not a composite number");
    }
    public static void Niven(int b)
    {
        int num=b;
        int sum=0;
        String x=String.valueOf(b);
        while(num!=0)
        {
            int rem=num%10;
            sum+=rem;
            num/=10;
        }
        if(b%sum==0&&x.length()!=1)
        System.out.println("It is a Niven number");
        else
        System.out.println("It is not a Niven number");
    }
    public static void main()
    {
        System.out.println("Enter a number");
        Scanner s=new Scanner(System.in);
        int num=s.nextInt();
        perfect_Q2.composite(num);
        perfect_Q2.Niven(num);
    }
}

LEAVE A REPLY

Please enter your comment!
Please enter your name here