08A-2016 – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Exam Questions-ICSE2016-08A #JAVA#7329    siteicon   siteicon  

Problem Statement - 08A-2016

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");
 }

 }
 }

Sample output
Enter a number: 126
Niven number


Share

CSKC| Created: 16-Apr-2019 | Updated: 16-Apr-2019|ICSE2016









Back