C language programming exercise reference answer chapter 5 (2) recursive functions

Source: Internet
Author: User

/* 5.10 write a function to obtain the nth entry of the Fibonacci series */

# Include "stdio. H"
Int maid (int n );

Void main ()
{
Int N;
Printf ("Calculate the nth entry of the Fibonacci series, enter n \ n ");
Scanf ("% d", & N);/* the value of N in vc6 must be less? */
Printf ("the % d of the Fibonacci series is % d", N, Fibonacci (n ));
}

Int Fibonacci (int n)
{
If (n = 1 | n = 2)
Return 1;
Else
Return maid (n-1) + maid (n-2 );

}

/* 5.11 write a function to determine whether an integer is a return number palindrome */

# Include "stdio. H"
Int palindrome (long N );

Void main ()
{
Long N;
Printf ("to determine whether an integer is a return number, enter n \ n ");
Scanf ("% lD", & N );
If (palindrome (n ))
Printf ("% LD is the number of replies", N );
Else
Printf ("% LD is not the number of replies", N );
}

Int palindrome (long N)
{
Int I, bit = 0;
Int A [10];
While (n! = 0)
{
A [bit] = n % 10;
N = N/10;
Bit ++;
}
For (I = 0; I <bit/2; I ++)
{
If (A [I]! = A [bit-1-i])
Return 0;
}
Return 1;
}

/* 5.12 write a function and calculate the N power of X. N is not less than 0 integer n> = 0

*/

# Include "stdio. H"
Double Power (Double X, int N );

Void main ()
{
Int N;
Double X;
Printf ("Calculate the N power of X, enter X and N, and separate numbers with spaces \ n ");
Scanf ("% lf % d", & X, & N );
Printf ("% lf % d times are % lf", X, N, power (x, n ));
}

Double Power (Double X, int N)
{
If (n = 0)
Return 1;
Else
Return (x * power (x, n-1 ));

}

 

/* 5.13 compile a function and use a recursive method to calculate the value of the n-order levender polynomial */

# Include "stdio. H"
# Include "conio. H"
Double P (Double X, int N );
Void main ()
{
Double X; int N;
Printf ("Calculate the polynomial of the n-order levender, enter X and N \ n ");
Scanf ("% lf % d", & X, & N );
Printf ("Result: % lf", P (x, n ));
Getch ();
}

Double P (Double X, int N)
{
If (n = 0) return 1;
If (n = 1) return X;
If (n> 1) Return (2 * N-1) * X-P (x, n-1)-(n-1) * p (x, N-2)/N;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.