CSCI 116 Fall 2013

Project #7: Computing Roots

 

 

Date Due: 28 October 2013

 

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 formula:

 

 

or, in our particular case,

 

 

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.

 

Here is a sample output:

 

Z:\Spring 2012\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 2012\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.