Write a java program to find the sum of the odd digit in a string.

Write a program to input a string array. The input may contain digits and alphabets (“de5g4G7R”). Extract odd digits from each string and find the sum and print the output.

For example, if the string is “AKj375A” then take 3+7+5=15 and not as 375 as the digit.

Assume the maximum length of the array is 20.

Input and Output Format

  • Input consists of an integer n, corresponds to the number of strings, followed by n Strings.
  • The output consists of an Integer.

Refer sample output for formatting specifications

Sample Input :

3

cram2nisumt1

al33k

d2t4H3r5

Sample Output :

15

(1+3+3+3+5)

Java Program to Find the sum of the odd digit in a string

Following are the steps to find the sum of the odd digits in a string:

  • Input number of strings from the users.
  • Create an array of strings.
  • Add string to an array and store it in s2.
  • Pass this array to the sum() method.
  • Inside the method, declare and initialize the variable sum to 0.
  • Iterate over the array using nested for loop. And get the character from the jth position and store it into variable c.
  • Now, check if the character is a digit then divide that digit by 2 to check if is odd. If it is then get that character value from the string and store it into a sum variable.
  • Repeat the whole process until the string is traversed.
  • At last, return the value of the sum.
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int s1 = sc.nextInt();
    String[] s2 = new String[s1];
    for (int i = 0; i < s1; i++) {
      s2[i] = sc.next();
    }
    System.out.println(getSum(s2));
  }

  public static int getSum(String[] s1) {
    int sum = 0;
    for (int i = 0; i < s1.length; i++)
      for (int j = 0; j < s1[i].length(); j++) {
        char c = s1[i].charAt(j);
        if (Character.isDigit(c)) {
          if (c % 2 != 0) {
            String t = String.valueOf(c);
            int n = Integer.parseInt(t);
            sum = sum + n;
          }
        }
      }
    return sum;
  }
}

Output

Sum of Odd-Even Digits in String in Java

Write a code to find the sum of odd and even digits in a string. Let us consider we have a string “Human1243” then the sum of odd digits would be 4 and even digits would be 6.

Input and Output Format

  • Input consists of an integer n, corresponds to the number of strings, followed by n Strings.
  • The output consists of an Integer.

Refer sample output for formatting specifications

Sample Input :

3

cram2nisumt1

al33k

d2t4H3r5

Sample Output :

Sum of Odd digits 15
Sum of Even digits 8

Program to find Sum of Odd-Even Digits in String

Following are the steps to find the sum of odd & even digits in a string:

  • Input number of strings from the users.
  • Create an array of strings.
  • Add string to an array and store it in s2.
  • Pass this array to the getSum() method.
  • Inside the method, declare and initialize the variable sumOdd and sumEven to 0.
  • Iterate over the array using nested for loop. And get the character from the jth position and store it into variable c.
  • Now, check if the character is a digit then divide that digit by 2 to check if is even. If it is then get that character value from the string and store it into a sumEven variable. Otherwise, store it in sumOdd variable.
  • Repeat the whole process until the string is traversed.
  • At last, return the value of the sumOdd and sumEven.
package com.demo2;

import java.util.Scanner;

public class SumOfOddDigits {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int s1 = sc.nextInt();
        String[] s2 = new String[s1];
        for (int i = 0; i < s1; i++) {
            s2[i] = sc.next();
        }
        getSum(s2);
    }

    public static void getSum(String[] s1) {
        int sumOdd = 0,sumEven = 0;
        for (int i = 0; i < s1.length; i++)
            for (int j = 0; j < s1[i].length(); j++) {
                char c = s1[i].charAt(j);
                if (Character.isDigit(c)) {
                    if (c % 2 != 0) {
                        String t = String.valueOf(c);
                        int n = Integer.parseInt(t);
                        sumOdd = sumOdd + n;
                    }else{
                        String t = String.valueOf(c);
                        int n = Integer.parseInt(t);
                        sumEven = sumEven + n;
                    }
                }
            }
        System.out.println("Sum of Odd digits "+sumOdd);
        System.out.println("Sum of Even digits "+sumEven);
    }
}

Output

Thus, in this way, we learned how to find the sum of odd digits in a string in Java.