Here is my sample harshad number genrator, if dont know or familiar with harshad numbers just take a look of this link... what is harshad?
package teamUndefined;
import java.io.*;
public class harshad {
public static void main(String[] args)throws IOException {
BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
String _input;
System.out.print("Enter Number: ");
_input = buff.readLine();
while (Integer.parseInt(_input) == 0
|| Integer.parseInt(_input) < 2
|| Integer.parseInt(_input) > 100000) {
System.out.print("Enter Number: ");
_input = buff.readLine();
}
String[] _num = new String[_input.length()];
for (int i = 0; i < _input.length(); i++) {
_num[i] = _input.substring(i, i + 1);
}
int _ans = 0;
for (int i = 0; i < _num.length; i++) {
_ans += Integer.parseInt(_num[i]);
}
_ans = Integer.parseInt(_input) % _ans;
if (_ans == 0){
System.out.print( _input + " is a Harshad Number");
}
else{
System.out.print(_input + " is not a Harshad Number");
}
}
}
Enjoy it.
Harshad number generator in java
on
0 comments:
Post a Comment