Niven and perfect number Solved program
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]
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);
}
}Related Posts
Continue learning with these related articles

User-Defined Methods in Java for ICSE Class 10: Parameters, Return and Overloading
ICSE Class 10 Computer Applications notes on user-defined methods: method prototype and signature, formal and actual parameters, return types, call by value vs call by reference, pure and impure methods, static methods and method overloading, with programs.

String Methods in Java for ICSE Class 10: 25 Output Questions with Answers
ICSE Class 10 Computer Applications practice on String class methods: length, charAt, indexOf, lastIndexOf, substring, replace, trim, compareTo, equals and more, with 25 board-style output questions and step-by-step answers.

Special Number Programs in Java for ICSE: Armstrong, Spy, Neon, Disarium, Pronic and More
Every special number asked in ICSE Computer Applications – prime, perfect, Armstrong, palindrome, automorphic, neon, spy, Niven, duck, buzz, Disarium, special and pronic numbers – with definitions, examples and one complete Java program that checks them all.