Question

Jave eclipse Wanna help Program 1 Algorithm Problem statement: Write a Java program to add up...

Jave eclipse
Wanna help

Program 1 Algorithm

Problem statement:

Write a Java program to add up the digits of a number entered by the user and output the result.   You may not use an array top perform this task. The program should be able to handle an integer of any size.

For example, if the number 1256 was entered, the sum would be:

SumOfDigits = 1 + 2 + 5 + 6 = 14

Solution:

The following Algorithm could be used:

                Define variables

                Get input number

                                Get each digit of input number

                                Add digit to sumOfDigits

                Output sumOfDigits

loop

remainder = inputNumber % 10

sumOfDigits = sumOfDigits + remainder

inputNumber = inputNumber – remainder

inputNumber = inputNumber / 10

end loop

Some helpful code snippets:

To get length of input number

                int lenOfNum = String.valueOf(inputNum).length();

To get number from keyboard

                int inNumber = Integer.parseInt(s.nextLine()); // What about validating input?


0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;
public class Main
{
   public static void main(String[] args) {
int inputNum, reminder, sum = 0;
Scanner s = new Scanner(System.in);
System.out.print("Enter the number:");
inputNum = Integer.parseInt(s.nextLine());
while(inputNum > 0)
{
reminder = inputNum % 10;
sum = sum + reminder;
inputNum = inputNum / 10;
}
System.out.println("Sum of Digits:"+sum);
}
}

Output:

Add a comment
Know the answer?
Add Answer to:
Jave eclipse Wanna help Program 1 Algorithm Problem statement: Write a Java program to add up...
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
  • 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...

  • 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...

  • Multiples of 9’s (JAVA) Using the logic behind the algorithm in Part 1, it’s easy to...

    Multiples of 9’s (JAVA) Using the logic behind the algorithm in Part 1, it’s easy to calculate the first nine multiples of 9 (9x1, 9x2, 9x3…, 9x9). Let’s use two examples. First, to determine the product of 9 times 6, perform the following algorithm: Step 1: Subtract 1 from the number being multiplied by 9 (in this case 6) to get the first digit of the product: 6 – 1 =  5. Step 2: Subtract that digit from 9 to get...

  • Using the above described algorithm, create a program that: (IN PYTHON) 1.Asks the user which type...

    Using the above described algorithm, create a program that: (IN PYTHON) 1.Asks the user which type of credit card he/she would like to find the checksum for. 2. Based on the user's choice of credit card, asks the user for the n digits of the credit card. [Get the input as a string; it's easier to work with the string, so don't convert to an integer.] 3. Using the user's input of the n digits, finds the last digit of...

  • Write the following program in Java using Eclipse. A cash register is used in retail stores...

    Write the following program in Java using Eclipse. A cash register is used in retail stores to help clerks enter a number of items and calculate their subtotal and total. It usually prints a receipt with all the items in some format. Design a Receipt class and Item class. In general, one receipt can contain multiple items. Here is what you should have in the Receipt class: numberOfItems: this variable will keep track of the number of items added to...

  • Write a Java program in Eclipse that reads from a file, does some clean up, and...

    Write a Java program in Eclipse that reads from a file, does some clean up, and then writes the “cleaned” data to an output file. Create a class called FoodItem. This class should have the following: A field for the item’s description. A field for the item’s price. A field for the item’s expiration date. A constructor to initialize the item’s fields to specified values. Getters (Accessors) for each field. This class should implement the Comparable interface so that food...

  • PYTHON Write a program to enter a valid variable till the user wants to end. The...

    PYTHON Write a program to enter a valid variable till the user wants to end. The program should display the count of positive, negative, zeros and letters (upper and lower case)/symbols (error handling) entered. Based on the problem, you need to design an algorithm as follows: 1. Get the user input until “n” is entered 2. Add to the positive if it is greater than zero 3. Add to the negative if it is less than zero 4. Add to...

  • Write a simple Java program with the following naming structure: Open Eclipse Create a workspace called...

    Write a simple Java program with the following naming structure: Open Eclipse Create a workspace called hw1 Create a project called hw1 (make sure you select the “Use project folder as root for sources and class files”) Create a class called Hw1 in the hw1 package (make sure you check the box that auto creates the main method). Add a comment to the main that includes your name Write code that demonstrates the use of each of the following basic...

  • Re-write this program into a javafx program, using javafx components only. NO awt or swing. import...

    Re-write this program into a javafx program, using javafx components only. NO awt or swing. import java.awt.Button; import java.awt.Label; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JPanel; public class DecimalBinary { public static void main(String[] args) { JFrame frame=new JFrame(); //Create a frame frame.setTitle("Decimal-Binary Converter"); //Set its label frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 300); //Set size fo frame JPanel panel=new JPanel(); //Create a panel to contain controls frame.add(panel); //add panel to frame panel.setLayout(null); Label decimalLabel=new Label("Decimal"); //Create label for decimal decimalLabel.setBounds(10,...

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