Computer Science, asked by sarafkrishnav0pav47f, 1 year ago

niven number program in Java

Answers

Answered by harshraj2717
3
Harshad number or Niven number is a number which is divisible by the sum of its digits. For example,

1) 21 is a Harshad number because it is divisible by the sum of its digits.

21 –> sum of digits –> 2+1 = 3 and 21 is divisible by 3 –> 21/3 = 7.

2) 111 is a Harshad number because it is divisible by the sum of its digits.

111 –> sum of digits –> 1+1+1 = 3 and 111 is divisible by 3 –> 111/3 = 37

3) 153 is a Harshad number. It is divisible by the sum of its digits.

153 –> sum of its digits –> 1+5+3 = 9 and 153 is divisible by 9 –> 153/9 = 17

How To Check Harshad Number Or Niven Number In Java?

Step 1 : Let inputNumber be the input number and sum be the sum of its digits.

Step 2 : First find the sum of digits of inputNumber and store it in sum.

while (inputNumber != 0) 
{
        lastDigit = inputNumber % 10;
        sum = sum + lastDigit;
        inputNumber = inputNumber / 10;
}

Step 3 : If inputNumber is divisible by the sum, then inputNumber is a Harshad number.

Java Program To Check For Harshad Number Or Niven Number


import java.util.Scanner;

 

public class HarshadNumberProgram

{   

    static void checkForHarshad(int inputNumber)

    {

        int copyOfInputNumber = inputNumber;

         

        int sum = 0;

         

        int lastDigit = 0;

         

        //Calculating the sum of digits of inputNumber

         

        while (inputNumber != 0)

        {

            lastDigit = inputNumber % 10;

             

            sum = sum + lastDigit;

             

            inputNumber = inputNumber / 10;

        }

         

        //Checking whether inputNumber is divisible by sum

         

        if((copyOfInputNumber % sum) == 0)

        {

            System.out.println(copyOfInputNumber+" is a Harshad number");

        }

        else

        {

            System.out.println(copyOfInputNumber+" is not a Harshad number");

        }

    }

     

    public static void main(String[] args)

    {

        Scanner sc = new Scanner(System.in);

         

        System.out.println("Enter any positive number : ");

         

        int inputNumber = sc.nextInt();

         

        checkForHarshad(inputNumber);

         

        sc.close();

    }

}

Output 1 :

Enter any positive number :
21
21 is a Harshad number

Output 2 :

Enter any positive number :
111
111 is a Harshad number

Output 3 :

Enter any positive number :
153
153 is a Harshad number

Output 4 :

Enter any positive number :
71
71 is not a Harshad number

Attachments:

harshraj2717: mark as brainliest
Answered by Anonymous
13

import java.util.*;

class niven_number

{

   public void main()

   {

       Scanner sc=new Scanner(System.in);

       System.out.println("Enter a number to check whether it is a niven number or not");

       int n=sc.nextInt();

       int d=0;

       int s=0;

       int cpy=n;

       while(n>0)

       {

           d=n%10;

           s=s+d;

           n=n/10;

       }

       if(cpy%s==0)

       {

           System.out.println(cpy+" is a niven number");

       }

       else

       {

           System.out.println(cpy+" is not a niven number");

       }

   }

}

Similar questions