Simple recursive exercises

Source: Internet
Author: User
1 # include <stdio. h> 2 // # include <stdlib. h> 3 int jiecheng (int I) {4 // ① calculate n! 5 if (I = 1) return 1; 6 return I * jiecheng (I-1); 7} 8 int Fibonacci (int I) {9 // ② calculate the I-th Fibonacci Series 10 if (I = 1) return 1; 11 if (I = 0) return 0; 12 Return Fibonacci (I-1) + Fibonacci (I-2); 13} 14 int power (int n, int K) {15 // ③ calculate n ^ k16 if (k = 1) return N; 17 return N * power (n, k-1); 18} 19 int gcd (int m, int N) {20 // ④ use the Euclidean algorithm to calculate the maximum common approx. of M and N 21 if (M % N = 0) return N; 22 return gcd (n, m % N ); 23 24} 25 int digitsum (int n) {26 // ⑤ enter a non-negative integer and return the number Sum of 27 words if (n = 0) return 0; 28 return (N % 10) + digitsum (N/10); 29} 30 int digitalroot (int n) {31 // ⑥ integer n numeric root 32 If (n <10) return N; 33 return digitalroot (digitsum (n); 34} 35 int comb (n, R) {36 // 7 calculate the composite number C (n, R); composite number C (n, R) = (P (n, R)/R !) = (N! /(N-R )! * R !)); 37 // The following formula exists: C (n, m) = C (N, N-m) = C (N-1 m-1) + C (n-1, m) 38 If (n = r | r = 0) return 1; 39 return comb (n-1, R-1) + comb (n-1, R ); 40} 41 42 void main () {43 44 printf ("① % d \ n", jiecheng (5); 45 printf ("② % d \ n ", fibonacci (6); 46 printf ("③ % d \ n", power (2, 3); 47 printf ("④ % d \ n", gcd (18, 24 )); 48 printf ("⑤ % d \ n", digitsum (12345); 49 printf ("⑥ % d \ n", digitalroot (12345 )); 50 printf ("7%d \ n", comb (6, 4); 51 52}

Result:


 

 

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.