Niven Number | Write a program in Java to check if the given number is NIven Number or not. | ISC Computer Science | ICSE Computer Application

Harshad Number : In recreational mathematics, a Harshad number (or Niven number), is an integer (in base 10) that is divisible by the sum of its digits.
Image is Unavailable

Algorithm

  1.   Start 
  2. x:=0
  3. Print "Enter a number"
  4. Read x
  5. if x%step 6 ==0, go to step 15 or else go to step 16
  6. t=x. Go to step 7
  7. digit:=0. Go to step 8
  8. sum=0. Go to step 9
  9. if t>0, go to step 10 or else go to step 14
  10. digit=t%10. Go to step 11
  11. sum=sum+digit. Go to step 12.
  12. t=t/10. Go to step 13
  13. Go to step 9
  14. return sum 
  15. Print "The given number is Niven Number". Go to step 17
  16. Print "The given number is not Niven Number"
  17. Stop

Program

Buffered Reader

import java.io.*;
class Niven
{
    int x;
    
    void accept() throws IOException
    {
        BufferedReader br=new BufferedReader (new InputStreamReader (System.in));
        System.out.println("Enter a number");
        x=Integer.parseInt(br.readLine());
    }
    
    int sumOfDigits()
    {
        int t=x, digit, sum=0;
        while(t>0)
        {
            digit=t%10;
            sum=sum+digit;
            t=t/10;
        }
        return sum;
    }
    
    void isNiven()
    {
        if(x%sumOfDigits()==0)
        {
            System.out.println("The given number is Niven Number");
        }
        else
        {
            System.out.println("The given number is not a Niven Number");
        }
    }
    
    void main() throws IOException
    {
        accept();
        isNiven();
    }
}

Scanner

import java.util.*;
class Niven
{
    int x;
    
    void accept() 
    {
        Scanner sc=new Scanner(Sysem.in);
        System.out.println("Enter a number");
        x=sc.nextInt();
    }
    
    int sumOfDigits()
    {
        int t=x, digit, sum=0;
        while(t>0)
        {
            digit=t%10;
            sum=sum+digit;
            t=t/10;
        }
        return sum;
    }
    
    void isNiven()
    {
        if(x%sumOfDigits()==0)
        {
            System.out.println("The given number is Niven Number");
        }
        else
        {
            System.out.println("The given number is not a Niven Number");
        }
    }
    
    void main() 
    {
        accept();
        isNiven();
    }
}

Documentation

Variable Description

Data Type Variable NameDescription
 intTo store the number entered by the user.
 int tTo act as a temp variable 
 intdigit To store the digit of the number 
 intsum To store the sum of digits of the number 

Method Description

 Return TypeMethod NameDescription
 voidaccept() To accept data from the user. 
 intsumOfDigits() To calculate the sum of digits of the number. 
 voidisNiven() To check whether the given number is Niven or not. 
 voidmain() To act as  an entry gateway for the compiler.

Output


Image is Unavailabe

Image is Unavailable




Share with all ISC Computer Science Students you know.

Comment below with all your program requests and blog suggestions.

Follow us on social media by clicking on these links. 
Instagram : @IscJavaCode
Twitter: @IscCode
Pinterest: @ISCJavaCode
Facebook: @ISCJavaCode


Stay Healthy
Stay Happy
Stay Helpful



Comments

You might also like:

Conversion of Complex Sentences to Simple Sentences

Conversion of Compound Sentences to Complex Sentences.

Conversion of Complex Sentences to Compound Sentences