Create a free account on Computer Sir Ki Class and unlock access to valuable learning resources.
Exam Paper Downloads: After registering and logging in, you can download exam question papers in PDF format to practice and prepare for your exams.
Student Support: Get assistance with your doubts, queries, and questions related to your studies. Our support is here to help you understand topics and resolve your learning difficulties.
Register today and make your exam preparation easier and more effective!
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.
Answer:
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");
}
}
}
Sample output
Enter a number: 126
Niven number
C. Sir| Created: 16-Apr-2019 | Updated: 16-Apr-2019|ICSE2016