Q1: Write A Recursive Function To Implement Newton Raphson Algorithm To Determine Square Root of A
Q1: Write A Recursive Function To Implement Newton Raphson Algorithm To Determine Square Root of A
Q1: Write a recursive function to implement newton Raphson algorithm to determine square root of a
number.
#include <bits/stdc++.h>
using namespace std;
double x = n;
double root;
int count = 0;
while (1) {
count++;
// Update root
x = root;
}
return root;
}
// Driver code
int main()
{
double n ;
float l = 0.00001;
cin>>n;
return 0;
}
EE-21162
Q2: Write a recursive function to find greatest common divisor of two numbers using Euclid’s remainder
algorithm.
#include <stdio.h>
if (b == 0) {
return a;
}
int q = a / b;
int r = a - q * b;
return 0;
}
EE-21162
Q3: Test for a number if its prime or composite using recursive method.
#include <bits/stdc++.h>
using namespace std;
// Returns true if n is prime, else
// return false.
// i is current divisor to check.
bool isPrime(int n, int i = 2)
{
// Base cases
if (n <= 2)
return (n == 2) ? true : false;
if (n % i == 0)
return false;
if (i * i > n)
return true;
// Check for next divisor
return isPrime(n, i + 1);
}
// Driver Program
int main()
{
int n = 15;
if (isPrime(n))
cout << "Yes";
else
cout << "No";
return 0;
}
EE-21162
Q4: Write a recursive function to implement the following expansion (precision up to 0.0001)
∞ n
(−1) x3 x5
sin x=∑
2 n+1
x =x− + −… for all x
n=0 ( 2 n+1 ) ! 3! 5!
#include <iostream>
#include <math.h>
using namespace std;
void cal_sin(float n)
{
float accuracy = 0.0001, denominator, sinx, sinval;
// Converting degrees to radian
n = n * (3.142 / 180.0);
float x1 = n;
sinx = n;
// holds the actual value of sin(n)
sinval = sin(n);
int i = 1;
do
{
denominator = 2 * i * (2 * i + 1);
x1 = -x1 * n * n / denominator;
sinx = sinx + x1;
i = i + 1;
} while (accuracy <= fabs(sinval - sinx));
cout << sinx;
}
// Main function
int main()
{
float n = 90;
cal_sin(n);
return 0;
}
EE-21162