Question
Write a program to accept a number and check and display whether it is a Niven number of not.
(Niven number is that number which is divisible by its sum of digits).
Example: Consider the number 126. Sum of its digits is 1 + 2 + 6 = 9 and 126 is divisible by 9.
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class program8
{
public static void main(String[] args)
{
int num=0,rem=0,temp=0,sumOfDigits=0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
num = sc.nextInt();
sumOfDigits = 0;
temp = num;
while (temp > 0)
{
rem = temp % 10;
temp = temp / 10;
sumOfDigits = sumOfDigits + rem;
}
if (num % sumOfDigits == 0)
{
System.out.println(num+" is a Niven number");
}
else
{
System.out.println(num +" is Not a niven number");
}
}
}