# pseudo code and algorithms

• December 3rd, 2012, 09:43 AM
oonagh
pseudo code and algorithms
Using Pseudo code, design an algorithm that can sum the digits of an integer in
the range 100-999. For example, given a number 734, the answer would be 7 + 3
+ 4, which equals 14. Given 103 the sum would be 1 + 0 + 3, which equals 4.
Hint: in order to extract each digit (units, tens, hundreds etc.) from a given
number, think about using the division (/) and, or modulus (%) of that number by
10 or multiples of 10.
Additional marks are available for an algorithm that can deal with any size of
number.
2. Complete the outline Java class below by,
Using the algorithm above to implement the sumOfDigits value method, which
accepts a number as a parameter and returns the sum of its digits.
Implement the main method that should prompt the user to input an integer
number then print the number and a message stating whether the sum of its
digits is equal to 10. Note - the user should be forced to enter a number between
100 and 999 inclusive. Sample program executions might look as follows

Input Number: 730
The sum of the digits of 730 is 10 and is equal to 10.

Input Number: 198
The sum of the digits of 198 is 18 and is NOT equal to 10.

// Sample outline class.
public class Sum
{
public static int sumOfDigits int number)
{
// complete method
}
public static void main(String[] args)
{
// complete method
}
}
• December 3rd, 2012, 09:48 AM
Norm
Re: pseudo code and algorithms
Do you have any specific questions about the assignment you posted?
What have you done so far?
• December 3rd, 2012, 09:51 AM
oonagh
Re: pseudo code and algorithms
how do you separate the 3 numbers?
i am new to java and really haven't a clue haven't attempted the question yet!
• December 3rd, 2012, 09:55 AM
Norm
Re: pseudo code and algorithms
If the number is in a String, the String class has methods for accessing each character in the String.
• December 3rd, 2012, 10:08 AM
oonagh
Re: pseudo code and algorithms
can you explain that in more detail please?

public class Sum
{
public static int sumOfDigits int number)
{
// complete method
}
public static void main(String[] args)
{
// complete method
}
}

that's the code started but i don't know what methods to put in
• December 3rd, 2012, 10:17 AM
Norm
Re: pseudo code and algorithms
Quote:

i don't know what methods to put in
First you need to make a list in pseudo code of what the program will do. When you have the list of steps, then use that to write the code.
• December 3rd, 2012, 10:23 AM
oonagh
Re: pseudo code and algorithms
ok thanks :)