|
Say that the user enters a 5. How is the program going to calculate 5 factorial? A good answer might be:With a counting loop that counts 1, 2, 3, 4, 5 and multiplies these values together. |
Flowchart of the Program
Another way to do this is to use N as the counter, and count it down until it reaches 2, so its values will be: 5, 4, 3, and 2. These values will be multiplied together one by one. There is no need to multiply by 1, so the loop's condition is N > 1. Here is a flowchart of the program. It tests if N is a correct value before it calculates N! This is somewhat trickier logic than previous programs have used. Study the flowchart until you see how it works. Don't worry about the details of Java (yet!) In fact, the flowchart says nothing about which language the program will be written in. The same flowchart could be used for Ada, C, C++, BASIC, FORTRAN, Pascal... and others. |
QUESTION 7:Try out some values of N. Does the flowchart. work correctly for N == -5 ? N == 0 ? N == 1 ? N == 4 ? Click Here after you have answered the question |