Daily C language exercises-1.1

1. Swap the contents of array A with the contents of array B. (the same size as the array)
char a[] = "Hello world!";
	char b[] = "Hello world!";

	char c[13] = "";

	int left;
	int right = sizeof(a) - 2;

	for (left = 0; left <= right; left++) {

		c[left] = a[left];
		a[left] = b[left];
		b[left] = c[left];
	}
	printf("a:%s\nb:%s\n", a, b);

2. Calculate the value of 1/1-1/2+1/3-1/4+1/5 +...+ 1/99 - 1/100.

float sum_1 = 0,
		sum_2 = 0,
		sum;
	int i;
	for (i = 1; i <= 100; i++) {
		if (i % 2 == 0) {
			sum_1 += 1 / i;
		}
		else {
			sum_2 += 1 / i;
		}
	}
	sum = sum_2 - sum_1;
	printf("1-1/2+1/3-1/4+...+1/99-1/100 = %f", sum);

3. Count how many times the number 9 appears in all integers from 1 to 100
int i =1,
		count = 0;
	for ( ; i <= 100; i++) {
		if (i % 10 == 9) {
			count++;
		}
		if (i / 10 == 9) {
			count++;
		}
	}
	printf("The number of occurrences of 9 is: %d\n", count);

Guess you like

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