Saturday, 16 August 2014

Console Based - Square Number Generator

Console Based - Square Number Generator



Adding to my recently uploaded prime numbers program. I have created a similar program showing all the square numbers from 0 to the persons choice. As can be seen above it works quite well, displayed are all the square numbers from 0 - 100000. I wont go into too much detail explaining the code I've written since its very similar to the prime numbers application's code. And this project was more as a proof of concept to myself than anything else.


Code

If you again I'm using two for loops the first one increments  by one after each cycle and is then tested by the second for loop to see if it is a square number. For example, lets say the outer loop is on a number of 63. The Boolean variable "square" is created and set to false. Then the inner for loop starts counting up numbers and each time it checks the number will go into the outer loop number ("count") with an evaluation the same as the number that went in and to make sure this is a factor and isn't a float value casted into an integer, dropping the decimal places, it also needs a remainder of zero. If that number is found then "square" is given a "true" value and the program continues. For the number 63 this will never happen since it isn't a square number, however, after the inner for loop has finished the outer for loop will increment "count" by one (raising it to 64) and the process will start again. When the variable "factor" reaches 8 it will divide perfectly into 64 (leaving a remainder of 0) which will make the "square" variable true and after it has checked for any other factors it will print the value 64 to the screen. 



Downloads
This project was good for just reinforcing what I had learned in the prime numbers code and was a great proof of concept.

The source file can be downloaded here. Thanks and until next time.

No comments:

Post a Comment