Question: Java programming question import java.util.Scanner; // Calculates the sum of odd digits of the input integer number // Example: number = 12345 Sum = 9
Java programming question
import java.util.Scanner;
// Calculates the sum of odd digits of the input integer number // Example: number = 12345 Sum = 9 (1+3+5) // number = 248 Sum = 0 // number = 65732098 Sum = 24 (5+7+3+9)
public class SumOfOddDigits { public static void main(String[] args) { String digits = "48596732 273457 13579 24680 3"; Scanner in = new Scanner(digits);
while (in.hasNextInt()) { int num; num = in.nextInt();
int sum = 0; int digit = 0;
//-----------Start below here. To do: approximate lines of code = 5 // Use a while loop. While num is greater than 0, use the modulus operator to strip off the next digit. // Check if this digit is odd, if so add it to sum variable // Finally, divide the num by 10 to throw away the current digit while(num > 0) { digit = num % 10; if(digit % 2 == 1) { // Only add digit if it is odd sum+= digit; } num = num / 10; //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions. System.out.println("The sum is " + sum); } System.out.println("Expected:"); System.out.println("The sum is 24"); System.out.println("The sum is 22"); System.out.println("The sum is 25"); System.out.println("The sum is 0"); System.out.println("The sum is 3"); } } }
Having trouble figuring out why its not working properly?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
