Numerology number Solution
Harry is very much interested in learning numerology with a programming Language. Help Harry to implement this task. Write a java program to find the sum of the digits and the numerology number(Multi-digit numbers are added and reduced to a single digit), followed by the total number of odd numbers and the total number of even numbers. Assume input is greater than zero and less than 10000000.
For example, if the given number is 7654 then,
- Sum of digits: 22 (7+6+5+4)
- Numerology number: 4 ((7+6+5+4 =22 => 2+2) sum of digits is again added and reduced to a single digit).
- Number of odd numbers: 2
- Number of even numbers: 2
Sample output:
Enter the number
86347
Sample output:
Sum of digits
28
Numerology number
1
Number of odd numbers
2
Number of even numbers
3
- Sum of digit = 28
- Numberogoy number = 1 : 28 => (2 + 8) = 10 => (1 + 0) = 1
SOLUTION
Program: Numerology number Solution in Java
import java.util.Scanner;
public class Main {
private static int getSum(long num) {
char[] chars = Long.toString(num).toCharArray();
int sum = 0;
for (char ch : chars) {
sum += Character.digit(ch, 10);
}
return sum;
}
private static int getNumerology(long num) {
String string = String.valueOf(num);
while (string.length() != 1) {
string = String.valueOf(getSum(Long.parseLong(string)));
}
return Integer.parseInt(string);
}
private static int getOddCount(long num) {
int oddCount = 0;
for (char ch : Long.toString(num).toCharArray()) {
if (Character.digit(ch, 10) % 2 != 0) {
++oddCount;
}
}
return oddCount;
}
private static int getEvenCount(long num) {
int evenCount = 0;
for (char ch : Long.toString(num).toCharArray()) {
if (Character.digit(ch, 10) % 2 == 0) {
++evenCount;
}
}
return evenCount;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number");
long num = scanner.nextLong();
System.out.println("Sum of digits");
System.out.println(getSum(num));
System.out.println("Numerology number");
System.out.println(getNumerology(num));
System.out.println("Number of odd numbers");
System.out.println(getOddCount(num));
System.out.println("Number of even numbers");
System.out.println(getEvenCount(num));
}
}
Related: