What is Niven Number

  • A number is said to be Niven number if it is divisible by the sum of its digits.
  • In mathematics Niven number is also known as Harshad number.
  • Example: 18, 126, 1729, etc.

Sample input: 126

Then sum of its digits= 1 + 2 + 6 = 9 and 126 is divisible by 9

// PROGRAM TO CHECK NIVEN NUMBER
import java.util.Scanner;
class Niven
 {
public static void main(String args[])
 {
int num,sum=0,rem,org_num; 
Scanner sc=new Scanner(System.in);
System.out.println("Enter any number");
num=sc.nextInt();
org_num=num;
while(num!=0)
 {
 rem=num%10;
 sum=sum+rem;
 num=num/10;
  }
 if(org_num%sum==0)
  System.out.println(org_num+" is Niven number");
 else
  System.out.println(org_num+" is not a Niven number");
 }
}