Computer Science, asked by bhaturachola454, 5 hours ago

write a program in java to input number of digits find sum of odd digits only . example input n = 3421 output sum of odd digits = 4​

Answers

Answered by debojyotimukherjee88
1

Answer:

import java.util.Scanner;

public class SumOfOddDigitsProgram {

 public static int findOddDigitSum(int number) {

    // declare variables

    int lastDigit = 0;

    int oddDigitSum = 0;

    // loop to repeat the process

    while(number!=0) {

        // find last digit

        lastDigit = number%10;

        // check last digit odd?

        if(lastDigit % 2 != 0) {

           // add it to sum

           oddDigitSum += lastDigit;

        }

        // remove last digit of number

        number = number / 10;

     }

     // return sum value

     return oddDigitSum;

 }

 public static void main(String[] args) {

     // declare variables

     int number = 0;

     int sumOfOddDigits = 0;

     // create Scanner class object

     // for reading the values

     Scanner scan =  new Scanner(System.in);

     // read inputs

     System.out.print("Enter an integer number::");

     number = scan.nextInt();

     // find sum of digits of number

     sumOfOddDigits = findOddDigitSum(number);

     // display result

     System.out.println("The sum of odd digits of"+

         +" the number "+number+" = "+ sumOfOddDigits);

     // close Scanner class object

     scan.close();

 }

}

Explanation:

Similar questions