home *** CD-ROM | disk | FTP | other *** search
/ C# & Game Programming - A…er's Guide (2nd Edition) / Buono 2nd Ed.iso / Chapter2 / 2.40 / 2.40.cs next >
Encoding:
Text File  |  2004-10-20  |  1.5 KB  |  45 lines

  1. /* Passing our first variable. */
  2. using System;
  3.  
  4. namespace Chapter2 {
  5.     class Class1 {
  6.         static void Main() {
  7.             string input; 
  8.             float number = 0, root = 0;
  9.  
  10.             Console.WriteLine("This program will find the "
  11.                 + "square root of any number\n"
  12.                 + "To continue just enter a number when prompted\n"
  13.                 + "Your results will appear after you press enter\n"
  14.                 + "To quit just enter the number \"1\"\n"
  15.                 + "Enter your first value now: ");
  16.             input = Console.ReadLine();
  17.             number = float.Parse(input);
  18.  
  19.             while (number > 1) {
  20.                 root = SquareRoot(number);
  21.                 Console.WriteLine(root);
  22.  
  23.                 Console.WriteLine("Enter your next value now: ");
  24.                 input = Console.ReadLine();
  25.                 number = float.Parse(input);
  26.             } 
  27.         } 
  28.  
  29.         // This function generates square roots using Newton's method      
  30.         static float SquareRoot(float n) {
  31.             float test1, test2;            
  32.             test1 = n - (float).01 - ((n - (float).01) * (n - (float).01) - n) 
  33.                 / (n * (n - (float).01));       
  34.             test2 = test1 - ((test1 * test1) - n) / ((n) * test1);            
  35.         
  36.             while (test1 != test2) {
  37.                 test1 = test2;
  38.                 test2 = test1 - (test1 * test1 - n) / ((n) * test1); 
  39.             }       
  40.             return (test2);   
  41.         } 
  42.     } 
  43. }
  44.  
  45.