Question

Lab Description : Write a program that will sum all of a numbers digits. You must use % for this lab to access the right most digit of the number. You will use /to chop off the right most digit. Sample Data 234 10000 Files Needed: DigitAdder.java JavaLoopLabRunner.java 9005 84645 8547 123456789 55556468 8525455 8514548 1212121212 Sample Output 14 27 24 45 34 35 15 12import static java.lang.System.* public class DigitAdder public static int go( int num ) return 0import static java.lang.System.* public class JavaLoopLabRunner public static void main String args ,11, 1212121212, 222222 (234, 10000, 11 1, 9003, 8445, 8547, 123456789, 55s /uncomment the class you need to test //as you need to test them //DigitAdder snew DigitAdder //DigitCounter s-new Digitcounter ) //DigitMath snew DigitMath) CountEvens s new CountEvensO Countodds snew Countodds) for( int val : cases System.out.println s.go( val)

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class DigitAdder {

    public static int go(int num) {
        int sum = 0;
        while (num > 0) {
            sum += (num % 10);
            num /= 10;
        }
        return sum;
    }

}
Add a comment
Know the answer?
Add Answer to:
Lab Description : Write a program that will sum all of a numbers digits. You must...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Lab Description Sort all words by comparing the length of each word. The word with the...

    Lab Description Sort all words by comparing the length of each word. The word with the smallest length would come first. If you have more than one word with the same length, that group would be sorted alphabetically Input: The data file contains a list of words. The first line in the data file is an integer that represents the number of data sets to follow Output: Output the complete list of words in order by length. Sample Data 10...

  • Lab Description: Using graphics, polygons, and arrays, draw the tree shown below. You can change the...

    Lab Description: Using graphics, polygons, and arrays, draw the tree shown below. You can change the tree anyway you like to make it your own. Sample Data Sec below Files Needed:: GraphicaRunner.java Tree.java Sample Output: import java.awt.Graphics; import java.awt.color; import java.awt.Polygon; import java.awt.Font; import java.awt.Canvas; public class Tree extends Canvas public Tree() setBackground (Color.WHITE) public void paint ( Graphics window) window. setColor (Color.RED); window.setFont (new Font("TAHOMA" ,Font.BOLD,12) window.drawstring("Lab14h Tree Lab", 50, 50) tree(window); IONaA , Font.BOL.D,12) public void tree(Graphics window)...

  • Lab Description Write a program that will search through an array to find the smallest number....

    Lab Description Write a program that will search through an array to find the smallest number. You must combine variables, ifs, and a loop to create a working method. There will always be at least one item in the list. Sample Data -99,1,2,3,4,5,6,7,8, 9,10,12345] [10,9,8,7,6,5,4,3,2,1-991 [10,20,30,40,50 -11818,40,30,20,10] 32767] [255, 255] 9,10-88,100,-555,10001 10,10,10,11,456] -111,1,2,3,9,11,20,30] 9, 8,7, 6,5,4,3,2,0-2,-989] [12,15, 18,21,23,1000] [250,19,17,15,13,11,10,9,6,3,2,1-4551 9,10,-8,10000,-5000,1000] Files Needed SmallestRunner.java RaySmallest.java Sample Output -99 -99 -11818 32767 255 -555 10 -989 -455 -5000 public class RaySmallest public...

  • This last lab teaches you to think and solve problems in the functional programming framework of...

    This last lab teaches you to think and solve problems in the functional programming framework of the Java 8 computation streams. Therefore in this lab, you are absolutely forbidden to use any conditional statements (either if or switch), loops (either for, while or do-while) or even recursion. All computation must be implemented using only computation streams and their operations! In this lab, we also check out the Java NIO framework for better file operations than those offered in the old...

  • LAB: How many negative numbers Write a program that generates a list of integers, and outputs...

    LAB: How many negative numbers Write a program that generates a list of integers, and outputs those integers, and then prints the number of negative elements. Create a method (static void fillArray(int [] array)) that uses Random to generate 50 values (Random.nextInt()), put each value in the array (make sure to pass the array in to this method as an argument). Write another method (static int printAndCountNeg(int [] array)) that prints each element’s value and count the number of negative...

  • Write a Java program to convert octal (integer) numbers into their decimal number equivalents (exactly the...

    Write a Java program to convert octal (integer) numbers into their decimal number equivalents (exactly the opposite of what you have done in Assignment 4). Make sure that you create a new Project and Java class file for this assignment. Your file should be named “Main.java”. You can read about octal-to-decimal number conversions on wikepedia Assignment 4 is at the bottom Objective Your program should prompt the user to enter a number of no greater than 8 digits. If the...

  • Write in JAVA Get Familiar with the Problem Carefully read the program description and look at...

    Write in JAVA Get Familiar with the Problem Carefully read the program description and look at the data file to gain an understanding of what is to be done. Make sure you are clear on what is to be calculated and how. That is, study the file and program description and ponder! Think! The Storm Class Type Now concentrate on the Storm class that we define to hold the summary information for one storm. First, look at the definition of...

  • LAB PROMPT: Lab 07 Hello future bakers and artisans of the fine gourmet. You love to...

    LAB PROMPT: Lab 07 Hello future bakers and artisans of the fine gourmet. You love to throw parties, and what goes better at a party than cake! The problem is that you only have enough flour to bake a limited number of cakes. In this lab, you will write a small program to figure out if you have enough flour to bake the number of cakes needed for the people attending. Step 1 - using final At the top of...

  • In this lab, you complete a partially written Java program that includes two methods that require...

    In this lab, you complete a partially written Java program that includes two methods that require a single parameter. The program continuously prompts the user for an integer until the user enters 0. The program then passes the value to a method that computes the sum of all the whole numbers from 1 up to and including the entered number. Next, the program passes the value to another method that computes the product of all the whole numbers up to...

  • In this lab, you complete a partially written Java program that includes two methods that require...

    In this lab, you complete a partially written Java program that includes two methods that require a single parameter. The program continuously prompts the user for an integer until the user enters 0. The program then passes the value to a method that computes the sum of all the whole numbers from 1 up to and including the entered number. Next, the program passes the value to another method that computes the product of all the whole numbers up to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT