Question
Harshad Number in a given range.
(A number is said to be the Harshad number if it is divisible by the sum of its digit.)
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class HarshadNumberInGivenRange
{
public static void main(String[] args)
{
int temp,lowerRange=0,upperRange=0,rem = 0, sum = 0,i=0,num=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the lower range");
lowerRange=sc.nextInt();
System.out.println("Enter the Upper range");
upperRange=sc.nextInt();
System.out.println("Harshad Numbers:");
for(i=lowerRange;i<=upperRange;i++)
{
sum=0;
if(i>0)
{
/*Make a copy of i and store it in variable temp*/
temp = i;
/*Calculates sum of digits*/
while(temp> 0)
{
rem = temp%10;
sum = sum + rem;
temp = temp/10;
}
//Checks whether number is divisible by sum of digits
if(num%sum == 0)
{
System.out.println(i+ " ");
}
}
}
}
}