Niven Number is a number which is divisible by sum of it’s digits.
Example 1: 18. 1+8 = 9. We observe that 18 is divisible by sum of it’s digits 9. Hence it is a Niven Number.
Example 2: 15. 1+5 = 6. Here 15 is not divisible by it’s sum of digits 6. Hence it is not a Niven Number.
Java
import java.util.*;
public class NivenNumber
{
public static void main(String args[])
{
int n=0,r=0,s=0,temp=0;
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number: ");
n=sc.nextInt();
temp=n;
while(temp>0)
{
r=temp%10;
s=s+r;
temp=temp/10;
}
if((n%s)==0)
{
System.out.println(n+" is a Niven Number");
}
else
{
System.out.println(n+" is not a Niven Number");
}
}
}
Java