In programming, there are often several ways to do the same task. If they work, they are "correct". But now, you need to start thinking about efficiency. In these assignments, the efficiency is generally unimportant because they're simple tasks that run quickly no matter how they're programmed.
What happens when you write a program that has to execute over and over, perhaps millions of times in a production environment? Think about your algorithm. It runs 4 loops that could run more than once. Let's look at that. For pennies, it could run up to 5 times. (At 5, it's a nickel. Also, it will "test" one extra time to exit the loop.) Nickels will test 2 times max. Dimes will test 3 times max. In any environment, it would have some impact, but not so much. So far, there's a maximum of 10 while loop tests, leaving quarters. Here's where the fun starts. For quarters, there's potentially no limit on how many passes through the while loop are required. It will depend on the value entered. If the number entered were $1000.00, that would mean about 4000 passes.
On the other hand, let's look at the execution of a program that used the modulo algorithm. Each coin would require one calculation to be executed exactly once for each type of coin. That means that the worst case execution is 4 calculations. (Actually, it can be done in 3, but I'll leave that for the readers to figure out. ;-) ). Also there will be less processing because there's no loop mechanism.
So now, think about a production environment where this has to execute 1 million times. Let's also say that the average amount is $25. In the while loop algorithm, that's 1M x 100 loop executions. In the modulo algorighm, it's 1M x 4 calculations. That means that the loop algorithm will run more than 25 times longer than the modulo algorithm. Multiply by a million and you could be looking at a program that runs in hours vs. seconds. That doesn't consider loop overhead (or any other statements in both algorithms.)
But you can relax for now. J ust learn the material being presented for now. Once you have the tools you need, the course will cover efficiency ( in a couple weeks.) Just try and master the current topics as they are presented. But do try to learn all of them. It's great to try different methods, but learn to use them all as you go. Don't skip any because you already know something. You may need it later. Best to master them all!
If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)