In the previous article, we have have discussed Java Program to Check Neon Number
In this article we are going to understand what Niven number is and how we can check whether a number is Niven or not in Java with examples.
Java Program to Check Niven Number
Niven numbers are integers in a given number that is divisible by the sum of its digits when written in that base.
Example: 2020 -> Niven number 6804 -> Niven number 9 -> Not a Niven number
In the above examples the numbers 2020 and 6804 both are Niven numbers while 9 is not.
Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples
Approach:
- We ask the user to enter a number which we store in an integer variable
num
. - We then use a while loop to iterate digits in the number and calculate the
sum
. - After coming out of loop if the resultant
sum
is equal to the number then the number is a Niven number, else it is not.
Let’s see different ways to check niven number.
Method-1: Java Program to Check Niven Number By Using Static Value
import java.util.Scanner; public class NivenNumber { public static void main(String args[]) { //A number is declared int num = 2020; //Temp is used to preserve orignal variable int temp = num, remainder, sum = 0; //Loop that iterates all digits and adds them while (temp > 0) { remainder = temp % 10; sum = sum + remainder; temp = temp / 10; } if (num % sum == 0) { System.out.println(num+" is a Niven Number"); } else { System.out.println(num+" is Not a Niven Number"); } } }
Output: 2020 is a Niven Number
Method-2: Java Program to Check Niven Number By Using User Input Value
import java.util.Scanner; public class NivenNumber { public static void main(String args[]) { //Taking the number as input from the user using scanner class Scanner scan = new Scanner(System.in); System.out.print("Enter a number : "); int num = scan.nextInt(); //Temp is used to preserve orignal variable int temp = num, remainder, sum = 0; //Loop that iterates all digits and adds them while (temp > 0) { remainder = temp % 10; sum = sum + remainder; temp = temp / 10; } if (num % sum == 0) { System.out.println(num+" is a Niven Number"); } else { System.out.println(num+" is Not a Niven Number"); } } }
Output: Enter a number : 2020 2020 is a Niven Number
Method-3: Java Program to Check Niven Number By Using Niven Number
import java.util.Scanner; public class NivenNumber { public static void main(String args[]) { //Taking the number as input from the user using scanner class Scanner scan = new Scanner(System.in); System.out.print("Enter a number : "); int num = scan.nextInt(); //calling the user defined method //to check Niven number or not. checkNumber(num); } //checkNumber() method to check niven number public static void checkNumber(int num) { //Temp is used to preserve orignal variable int temp = num, remainder, sum = 0; //Loop that iterates all digits and adds them while (temp > 0) { remainder = temp % 10; sum = sum + remainder; temp = temp / 10; } if (num % sum == 0) { System.out.println(num+" is a Niven Number"); } else { System.out.println(num+" is Not a Niven Number"); } } }
Output: Enter a number : 2020 2020 is a Niven Number
The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.
Related Java Programs: