Write a static method named numberoddSum that accepts a posi
Solution
Hi, PLease find my implementtion.
Please let me know in case of any issue.
import java.util.Scanner;
public class OddDigitsSum {
public static int numberOddSum(int n){
int sum = 0; // variable to store sum of odd digits
// if n is negative, making it positive
if(n < 0)
n = -n;
while( n > 0){
int digit = n%10; // getting current digit
if(digit % 2 == 1)
sum = sum + digit;
n = n/10;
}
return sum;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print(\"Enter a integer: \");
int number = sc.nextInt();
System.out.println(\"Odd digit sum: \"+numberOddSum(number));
}
}
/*
Sample run:
Enter a integer: 4321234
Odd digit sum: 7
*/

