Skip to main content

Niven number in java


A number is said to be niven or harshad if the number is divisible by the sum of its digits.
EXAMPLE-135
The sum of digits are: 1 + 3 + 5                                                                  135/9=15
A simple java program to check whether a number is harshad / niven or not can be made with the help of while loop.
For this, firstly we extract the digits of the number and then we sum up all the digits.After all the digits are summed then we check whether rhe number when divided by the sum of its digits leaves any remainder or not.
If the remainder is zero then number is niven / harshad number else not.

ALGORITHM-
STEP 1: Start
STEP 2 : print "enter a number"
STEP 3 : taking input of integer in n
STEP 4 : int s = 0
STEP 5 : int no = n
STEP 6 : repeat STEP 7 to STEP 9 until no!=0
STEP 7 : int d=no%10
STEP 8 : s=s+d
STEP 9 : no=no/10
STEP 10: if (n%s==0) then goto STEP 11 else goto STEP 12
STEP 11 : print n+"is a harshad number"
STEP 12 : print n+"is not a harshad number"
STEP 13 : end

JAVA CODE-
import java.util.*;
public class harshad
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter a number");
int n=sc.nextInt();
int s=0;
int no=n;
while(no!=0)
{
int d=no%10;
s=s+d;
no=no/10;
}
if(n%s==0)
{
System.out.println(n+" is a harshad number");
}
else
{
System.out.println(n+" is not a harshad number");
    }
  }
}

Output-
enter a number
135
135 is a harshad number

SIMILAR JAVA CODES-
If you find this helpful please share

Comments

Popular posts from this blog

Palindrome number in java

A number is said to be palindrome if it reads both forward and backwards same. Example- 175571 454 A program for palindrome number can be made by simply extracting digits of the number in reverse order and adding them with their place value and then checking is it same as the original number or not. To extract a digit in revers order and adding with place value we simply follow the below code. while(n>0) { int d=n%10; rev=(rev*10)+d; n=n/10; } To explain this let us take an example 657 Then  For first iteration =  n=657                                     d=657%10=7                                     rev=(0*10)+7=7 For second iteration = n=65                                         ...

Java program for prime numbers

A prime number is the number which is is  divisible by itself and not by other numbers. A program to check prime number can be made by using for and while loop combination . This article contains java program for finding prime number ,write a java program for prime number between 1- 100 ,simple program to find prime factorial of a number in java. EXAMPLE 1 - program to check whether a number is prime number  or not. import java.util.*; public class pr { public static void main() { Scanner sc=new Scanner(System.in); System.out.println("enter number"); int num=sc.nextInt(); int d=0; for(int i=2;i<=num;i++) { if(num%i==0) d++; } if(d==0) System.out.println("it is a prime number"); else System.out.println("it is not a prime number"); } } Output enter number 23 it is a p...

What is magic number in java ?

A number is said to be magic number if the repetitive sum of digits of number is equal to 1 or a single digit number . If the repetitive sum ends up  as 1 then the number is a magic number, else not. For example- n= 289;.   2+8+9 = 19      19;.      1+9 = 10      10;.      1+0= 0       1 Since the number ends in 1, hence 289 is a magic number. Magic number in java is the java program to check whether a number is magic number or not . For this we do repetitive addition of the digits of the number till the number is a two or more digit number. If after repetitive adding digits of the number ends up to 1 then the loop stops and a message is displayed. For this program we need to extract digits of the number for this we use while loop and add this digit . Code snippet to get digits of the number - while(n!=0) {        int d = n%10;         s=s+d;   ...