File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed 
C++/Algorithms/Mathematical Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change 1+ #include  < iostream> 
2+ #include  < math.h> // for log10(), fabs(), pow()
3+ 
4+ using  namespace  std ; 
5+ 
6+ #define  DBL_EPSILON  2.2204460492503131e-16 
7+ 
8+ // These 2 functions will accept user input and return the resulting Square Root of that user input
9+ 
10+ // Iterative Function
11+ double  sqrtIterative (double  x)
12+ {
13+  double  sqrt = x * pow (0.3 , log10 (x));
14+  for  (int  i = 0 ; i < 5 ; i++)
15+  sqrt = (sqrt + (x / sqrt)) * 0.5 ;
16+  return  sqrt;
17+ }
18+ 
19+ // Recursive function
20+ double  sqrtRecursive (double  num, double  prev)
21+ {
22+  double  next = (prev + num / prev) / 2 ;
23+  if  (fabs (next - prev) < DBL_EPSILON * next)
24+  return  next;
25+  return  sqrtRecursive (num, next);
26+ }
27+ 
28+ int  main (void )
29+ {
30+ 
31+  // Iterative Function
32+  cout << " *************************Iterative Function*************************" 
33+  cout << " Please enter a number : " 
34+  double  it;
35+  cin >> it;
36+  double  answerIterative = sqrtIterative (it);
37+  cout << " The Square Root is : " 
38+  << endl;
39+ 
40+  // Recursive Function
41+  cout << " *************************Recursive Function*************************" 
42+  cout << " Please enter a numbers : " 
43+  double  rc;
44+  cin >> rc;
45+  double  answerRecursion = sqrtRecursive (rc, 1 );
46+  cout << " The Square Root is : " 
47+ }
                         You can’t perform that action at this time. 
           
                  
0 commit comments