Write a static method named numberoddSum that accepts a posi

Write a static method named numberoddSum that accepts a positive integer returns the sum of its odd digits. For example, the call numberOddSum (1436527) should return 16.

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

*/

 Write a static method named numberoddSum that accepts a positive integer returns the sum of its odd digits. For example, the call numberOddSum (1436527) should
 Write a static method named numberoddSum that accepts a positive integer returns the sum of its odd digits. For example, the call numberOddSum (1436527) should

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site