C language daily small exercises (building wheels)-1.7

1. Recursive and non-recursive implementations to find the nth Fibonacci number respectively. 

#include <stdio.h>
#include <Windows.h>

int getFib(int n) {
	int first = 1;
	int second = 1;
	int third = 1;
	while (n > 2) {
			third = first + second;
			first = second;
			second = third;
			n--;
	}
	return third;
}

int fib(int n) { //recursion
	if (n <= 2)
		return 1;
	else
		return fib(n - 1) + fib(n - 2);
}

int main() {
	int ret = getFib(4);
	int _ret = fib(4);
	printf("%d\n%d\n", ret, _ret);

	system("pause");
	return 0;

}

2. Write a function to implement n^k, using recursion 

#include <stdio.h>
#include <Windows.h>
//implement n^k;
int _power(int n, int k) {
	
	if (k == 0)
		return 1;
	else
		return n * _power(n,k - 1);
}
int main() {
	int ret = _power(2, 3);
	printf("%d\n", ret);

	system("pause");
	return 0;
}

3. Write a recursive function DigitSum(n), input a non-negative integer, and return the sum of the numbers that make up it. For example, calling DigitSum(1729) should return 1+7+2+9, and its sum is 19 

#include <stdio.h>
#include <Windows.h>
int DigitSum(int n) {

	if (n == 0)
		return 0;
	else
		return (n % 10) + DigitSum(n / 10);
}

int main() {
	int n;
	printf("Please enter a positive integer:\n");
	scanf_s("%d", &n);

	int ret = DigitSum (n);
	printf("%d\n", ret);

	system("pause");
	return 0;
}
4. Write a function reverse_string(char * string) (recursive implementation) 
to implement: reverse the characters in the parameter string. 

Requirement: String manipulation functions  in the C library cannot be used .

#include <stdio.h>
#include <windows.h>

char * reverse_string(char *p){
	int n = 0;
	char temp;

	char *q; //Save the starting p with q
	 q = p
	
	while (*p != 0){ //Calculate the size of the string  
		n++;
		p++;
	}

	if (n > 1){   
		temp = q[0];
		q[0] = q[n - 1];
		q[n - 1] = '\0';

		reverse_string(q + 1);
		q[n - 1] = temp;
	}
	return q;
}

int main() {

	char str[] = "i love u forever";
	printf("The original string is: %s\n", str);
	printf("The reversed string is: %s\n", reverse_string(str));

	system("pause");
	return 0;
}

5. Recursive and non-recursive implementation of strlen respectively 

#include <stdio.h>
#include <windows.h>
#include <assert.h>

int my_strlen_two(const char* str){
	// recursive implementation  
	assert(str != NULL);
	if (*str)
		return 1 + my_strlen_two(str + 1);
	else
		return 0;
}

int my_strlen_one(const char* str){
	// non-recursive implementation  
	int count = 0;
	assert(str != NULL);
	while (*str){
		count++;
		str++;
	}
	return count;
}

int main(){
	int len_one = my_strlen_one("abcdef");
	int len_two = my_strlen_two("abcdef");

	printf("len_one = %d\n", len_one);
	printf("len_two = %d\n", len_two);

	system("pause");
	return 0;
}

6. Recursive and non-recursive realization of the factorial of n respectively 

#include <stdio.h>
#include <Windows.h>

int fact(int n) {//recursive
	if (n == 1)
		return 1;
	else
		return n * fact(n - 1);
}
int my_fact(int n) {//iteration
	
	int j = 1;
	for (int i = 2; i <= n; i++) {
		j = j * i;
	}
	return j;
}
int main() {
	printf("%d\n", fact(3));
	printf("%d\n", my_fact(3));

	system("pause");
	return 0;
}

7. Recursive way to print each bit of an integer 

#include <stdio.h>
#include <Windows.h>
void show(int n) {
	if (n < 10) {
		printf("%d\n", n);
	}
	else {
		printf("%d\n", n % 10);
		show(n / 10);
	}
}
int main() {
		show(234);
		
		system("pause");
		return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326521127&siteId=291194637