Write a program to input a number check and display whether it is a Niven number or not Example – sample input 126
Sum of its digits = 1 + 2 +6 =9 and 126 is divisible by 9
Answers
Answered by
1
Answer:
Harshad (Or Niven) Number
- An integer number in base 10 which is divisible by sum of it digits is said to be a Harshad Number. An n-harshad number is an integer number divisible by sum of its digit in base n.
- Below are first few Harshad Numbers represented in base 10:
- 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 18, 20………
- Given a number in base 10, our task is to check if it is a Harshad Number or not.
Answered by
4
Question:
Write a program to input an integer and check whether it is Harshad or Niven number or not. A number is said to be Harshad if it is divisible by the sum of the digits of that number, example 126 and 1729.
Solution:
Language used: Java
import java.util.*;
public class NivenNo
{
static void main()
{
Scanner sc=new Scanner(System.in);
int d,s=0,n,t;
System.out.println("Enter an integer:");
n=sc.nextInt();
t=n;
while(t!=0)
{
d=t%10;
s=s+d;
t=t/10;
}
if(n%s==0)
System.out.println("Harshad Number");
else
System.out.println("Not a Harshad Number");
}
}
Similar questions