0% found this document useful (0 votes)
60 views5 pages

Q1: Write A Recursive Function To Implement Newton Raphson Algorithm To Determine Square Root of A

The document contains 4 questions and code snippets providing recursive solutions: 1) A recursive function to calculate the square root of a number using the Newton-Raphson method. 2) A recursive function to find the greatest common divisor (GCD) of two numbers using Euclid's algorithm. 3) A recursive function to test if a number is prime or composite by checking if it is divisible by any number between 2 and the square root of the number. 4) A recursive function to calculate the sine of a number by implementing its power series expansion to within 0.0001 precision.

Uploaded by

izhan kal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views5 pages

Q1: Write A Recursive Function To Implement Newton Raphson Algorithm To Determine Square Root of A

The document contains 4 questions and code snippets providing recursive solutions: 1) A recursive function to calculate the square root of a number using the Newton-Raphson method. 2) A recursive function to find the greatest common divisor (GCD) of two numbers using Euclid's algorithm. 3) A recursive function to test if a number is prime or composite by checking if it is divisible by any number between 2 and the square root of the number. 4) A recursive function to calculate the sine of a number by implementing its power series expansion to within 0.0001 precision.

Uploaded by

izhan kal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

EE-21162

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 squareRoot(double n, float l)


{

double x = n;

double root;

int count = 0;

while (1) {
count++;

// Calculate more closed x


root = 0.5 * (x + (n / x));

// Check for closeness


if (abs(root - x) < l)
break;

// Update root
x = root;
}

return root;
}

// Driver code
int main()
{
double n ;
float l = 0.00001;
cin>>n;

cout << squareRoot(n, l);

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>

int euclid(int a, int b)


{

if (b == 0) {
return a;
}

int q = a / b;
int r = a - q * b;

return euclid(b, r);


}
#include <iostream>
int main()
{
int a ;
int b ;
std::cin>>a >>b;

printf("Euclid(%d, %d) = %d", a, b, euclid(a, 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

You might also like