Java program that uses a loop to compute the sum of all odd digits of an input. (For example, if the input is 32677, the sum would be 3 + 7 + 7 = 17.)
Posted by Samath
Last Updated: April 03, 2022

Write a Java program that uses a loop to compute the sum of all odd digits of an input. (For example, if the input is 32677, the sum would be 3 + 7 + 7 = 17.)

Code:

```import java.util.Scanner;

public class Main
{
public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int num = input.nextInt();
int k = num;
input.close();
int result = 0;
while (num > 0) {
int digit = num % 10;
if (digit % 2 != 0) {
result += digit;
}
num /= 10;
}
System.out.printf("Sum of %d odd digits: %d", k, result);
}
}
```
Java program that uses a loop to compute the sum of all even numbers between 2 and 100 (inclusive)
Samath | Apr 03, 2022
Java program that uses a loop to compute the sum of all squares between 1 and 100 (inclusive)
Samath | Apr 03, 2022
Java program that uses a loop to compute the sum of all even numbers between a and b (inclusive), where a and b are inputs
Samath | Apr 03, 2022
Write a Python function to calculate the sum of digits of a number.
MaryJns | Aug 17, 2024
Recursive function that find the sum of digits in a integer using C++
Samath | Dec 24, 2014
JavaScript program that return the sum of all odd Fibonacci numbers that are less than or equal to n
SceDev | Mar 28, 2024
How do you find the sum of elements in a list using a loop in Python?
FrankMl | Aug 13, 2024
How can you find the sum of elements in a list using a while loop in Python?
HenryPk | Aug 18, 2024
How can you find the sum of elements in a list using a for loop in Python?
EveClark | Aug 17, 2024
C Program to Compute sum of the array elements using pointers
Samath | Jan 02, 2017
C program that prompts the user for a positive integer and then computes the sum of all the digits of the number
Samath | May 04, 2016
Sum of two values in C
Samath | Nov 09, 2014
C Program to find sum of two numbers
Samath | Jan 02, 2017
C++ Calculate the sum from 1 to 100
Samath | Jan 05, 2017