# The Wright Stuff

 Gliding Weather with Attitude [Computing] Web Help About Me

## More Pseudocode

Here's another example of using pseudocode

You are required to write a program which will take as its input a positive integer of up to nine digits, find the last number, and raise each indvidual digit to the power of that last digit. Then add all these numbers together and check if the answer is the same as the original number. If it is the same, print a message to the screen. Here's an example to show what it means. The user enters the number 1232. The last digit is 2. If 12 + 22 + 32 + 42 = 1232, print "It's a hit" on screen. Otherwise print "Not a magic number".

Here's the first attempt at pseudocode.

 Read the input Find last digit Find all the individual digits Calculate the total of all the powers If the total equals the original Print It's a hit Else Print Not a magic number end if

Then after a few minutes thought we refine the above to the following.

 Read the input Find last digit Remove the last digit Find the "new" last digit Repeat the above until all digits dealt with Calculate the total of all the powers If the total equals the original Print It's a hit Else Print Not a magic number end if

That is the basic plan for the program! Now all we have to do it expand on each part using stepwise refinement, to get a better view of how the program will look. We need a way to slice of the last number to get hold of it and to leave the number with the last digit sliced off. It helps if we remember that when working with integers we can use modular division - divide by a number and get the remainder, and that integer division throws away the remainder. For example, 129 divided by 10 as integer division gives an answer of 12. But 129 mod 10 gives an answer of 9 (the remainder). The other point to remember is that as the user can enter any number from 0 to 999999999, we don't know how many digits we will be dealing with, so the program must be able to handle any number in the range. So here's the first few bits of refinement to our ideas.

 Read the input input = original lastOne = original mod 10 /* get the remainder new = original / 10 /* integer division, no remainder eigth = new mod 10 /* get next remainder new = new / 10 /* integer division, no remainder seventh = new mod 10 /* and so on new = new / 10 sixth = new mod 10 new = new / 10 fifth = new mod 10 new = new / 10 fourth = new mod 10 new = new / 10 third = new mod 10 new = new / 10 second = new mod 10 first = new / 10 Calculate the total of all the powers If the total equals the original Print It's a hit Else Print Not a magic number end if

So now we have a way of getting the current last digit and then removing that totally from the original number. To show it working, here are a few of the steps.

1232 mod 10 gives 2, then 1232 / 10 give 123. Then 123 mod 10 give 3 and 123 / 10 gives 12. Then 12 mod 10 gives 2 and 12 /10 gives 1. Next 1 mod 10 gives 1, and 1 /10 gives 0. All further divisions will give us 0 as well.

So now all we need to do is the calculation of the numbers raised to the power of the last digit. Every programming language has a maths function that does this for you. so we can write a shorthand version of this to complete the pseudocode. We simply write maths(number to the power of lastOne) as a shorthand for whatever the language you are using has for its maths function. We don't need to know how to write the maths function just now, we will find that out when we pick up a text book.

 Read the input input = original lastOne = original mod 10 /* get the remainder new = original / 10 /* integer division, no remainder eigth = new mod 10 /* get next remainder new = new / 10 /* integer division, no remainder seventh = new mod 10 /* and so on new = new / 10 sixth = new mod 10 new = new / 10 fifth = new mod 10 new = new / 10 fourth = new mod 10 new = new / 10 third = new mod 10 new = new / 10 second = new mod 10 first = new / 10 total = maths(first to power of lastOne) + maths(secondto power of lastOne) + maths(third to power of lastOne) +maths(third to power of lastOne) + maths(fourth to power of lastOne) + maths(fifth to power of lastOne) +maths(sixth to power of lastOne) + maths(seventh to power of lastOne) + maths(eigth to power of lastOne) + maths(ninth to power of lastOne) If (total equals original) Print It's a hit Else Print Not a magic number End if

The maths part depends on a simple fact - zero to the power of any number is zero, so it handles all the zeros that are generated by division when we run out of actual digits.

So now we might look at expanding the Read input line, to check the number is within the allowed range.

 Read input input value = original while(original less than zero OR original value greater than 999999999) Print "Sorry we need integers with a value between zero and 999999999.. Please try again." read input input value = original end while (followed by the rest of the pseudocode above)

The plan is complete. So now all we have to do is look in a text book for any help with the syntax of a particular command and then write the program. This pseudocode will work for Visual Basic, C, C++, Java, PHP, Pascal, Delphi and so on. Because it is a plan. And any program you plan in advance is bound to work. And barring typos, the resulting program will work first time! (As a test, 153 = 13 + 53 + 33, and the only other numbers where this is true are 1, 1634, 9474 and of course, as I'm sure you knew, 9800817.)

Plan your programs - you know it makes sense.