Write a program to accept a number and check and display whether it is a Niven number of not. [15]
(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.
Solution
TC++ #7329
import java.util.Scanner;
public class NivenNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
int sumOfDigits = 0;
int copyOfNum = number;
while (number > 0) {
int remainder = number % 10;
number = number / 10;
sumOfDigits = sumOfDigits + remainder;
}
if (copyOfNum % sumOfDigits == 0) {
System.out.println("Niven number");
} else {
System.out.println("Not a niven number");
}
}
}