Computer Science, asked by tanishagrahari2018, 1 year ago

write a Java program to tell whether the number is Niven or not

Answers

Answered by kavyasamak
2
import java.util.*;

class EvenOrNot
{
public static void main(String args[])
{
int n;
Scanner y = new Scanner(System.in);
n=y.nextInt();
if(n%2==0) System.out.println(n+" is even");
else System.out.println(n+" is not even");
}
}

tanishagrahari2018: no it is not an even or odd number program it is a Niven number program
kavyasamak: ok.. then take int variable as input , say n.. store this n value in another variable say m .. ie, m=n.. let int variable s be initialized to zero..int s=0... now you have to sum the digits of n .. for this use while(n!=0){ s=s+n%10; n=n/10; }... here in this loop you will end up with the sum of the digits in the given number n... now use if condition ... if (m%s==0) then print Niven no else not...
Answered by Anonymous
2

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