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
Sum of First n Natural Numbers in Java (Loop and Formula)
Find the sum of the first n natural numbers in Java with a for loop, a while loop and the formula n(n + 1)/2, with a dry run and the sums of even numbers, odd numbers, squares and cubes.
Prime Number Program in Java: Check a Number and Print Primes in a Range
Check whether a number is prime in Java by counting its factors, a faster square-root method with isPrime(), and a program that prints all prime numbers in a range.

Encapsulation, Access Specifiers and Scope of Variables in Java for ICSE Class 10
ICSE Class 10 Computer Applications notes on encapsulation: private, default, protected and public access, data hiding with getter and setter methods, instance vs static (class) variables, local variables and scope, with a solved BankAccount program.