CSCI 116

Project #7: Computing Roots

 

 

 

For this program you are going to write a program to find the nth root of a number.  Your program should ask the user for a number, and a root and print that root of the number.  (For example, the user might enter 81 and 4, and your program would print that the 4th root of 81 is 3.)  Your program should use Newton’s method to compute the root using successive approximation.  As you may recall, in class we derived the formula that to find a zero of the equation xk – a = 0, you make a guess for x0 (for example, a/k), and then iterate using the formula1:

 

 

or, in our particular case,

 

 

where k is the root we want, and a is the number we want the root of.  Iterate until the value doesn’t change any more, and then print the result using scientific notation to 15 digits.  After finding and printing the root, your program should ask the user for another number and root, and should repeat the process.  Your program should exit when the user enters 0 for the root.

 

Your program must use functions to compute succeeding terms, and to do the exponentiation.

 

Here is a sample output:

 

Z:\Spring 2015\CSCI 116>newton

 Enter a number and a root:

228886641 4

 The   4th root of  228886641 is  123

 Enter a number and a root:

4096 6

 The   6th root of  4096 is  4

 Enter a number and a root:

2.25 2

 The square root of  2.25 is  1.5

 Enter a number and a root:

15625 3

 The cube root of  15625 is  25

 Enter a number and a root:

0 0

 

Z:\Spring 2015\CSCI 116>

 

Note that the square root printed out as “square root” instead of “2th root”.  Your program should detect first, second, and third roots and print them out as “first”, “square”, and “cube” roots.

 

 

1NOTE: If the formulas don’t show up, try a different browser or else use the pdf version.