While loop


      Write a Program in Java to input a number and check whether it is a Harshad Number or Niven Number or not.
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 18, 20, 21, 24, 27, 30, 36, 40, 42, 45, 48, 50
import java.util.*;
class HarshadNumber {   public static void main(String args[])
    {  Scanner sc = new Scanner(System.in);  
        System.out.print("Enter a number : ");
        int n = sc.nextInt();
        int c = n, d, sum = 0;
        //finding sum of digits
        while (c>0)
        {  d = c%10;  sum = sum + d;
            c = c/10;}
    if(n%sum == 0)
System.out.println(n+" is a Harshad Number.");
       else
   System.out.println(n+" is not a Harshad Number.");     
    }

No comments:

Post a Comment

what is chain constructor in java

  In Java, chain constructor refers to a technique where one constructor calls another constructor within the same class. This is achieved b...