Please post the complete program here to make it easier to copy to an editor.
What data types are the variables that you are using ?
Have you tried printing the intermediate results to check that they are correct ?
By the way, you could use a for loop for much of the program rather than repeating the same code over and over again.