Given a starting number and the ending number the task is to find the all the Niven Number present in that given range and also to display it’s frequency.
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 NivenNumberRange
{
public static void main(String args[])
{
int start=0,end=0,temp=0,frequency=0,r=0,s=0;
Scanner sc=new Scanner(System.in);
System.out.print("Enter a starting range: ");
start=sc.nextInt();
System.out.print("Enter a ending range: ");
end=sc.nextInt();
System.out.println("Niven Numbers in the given range are:-");
for(int i=start;i<=end;i++)
{
temp=i;
while(temp>0)
{
r=temp%10;
s=s+r;
temp=temp/10;
}
if((i%s)==0)
{
System.out.println(i);
frequency++;
}
s=0;
}
System.out.println("Frequency: "+frequency);
}
}Java