Harshad Number

 //Wap to accept a number and check it is Harshad number or not.

//A number is said to be Harshad, if the number is divisible by sum of its digits.

import java.util.*;

class harshad

{//class harshad opens

    public static void main(String[]args)

    {//main method opens

        Scanner sc=new Scanner(System.in);

        System.out.println("Enter a no");

        int n=sc.nextInt();//variable n to accept the no 156

        int k=n,r=0,s=0;//variable k to store the no,variable r to extrct the digits,variable s to store the sum

        while(n>0)//156>0 true//15>0 true//1>0 true

        {

            r=n%10;//156%10=6//15%10=5//1%10=1//0>0 false

            s=s+r;//0+6=6//6+5=11//11+1=12

            n=n/10;//156/10=15//15/10=1//1/10=0

        }

        if((k%s)==0)//156%12==0 true

        System.out.println(k+" is  harshad no");

        else

        System.out.println(k+" is not a harshad no");

    }//main method closes

}//class closes


No comments:

Post a Comment

any problem in any program comment:-

Second largest number in java using ternary operator

 //Largest second number using ternary operator public class Main { public static void main(String[] args) { int a=5,b=6,c=7; int ...