C语言日常小练习(造轮子)-1.6

1.实现一个函数,打印乘法口诀表,口诀表的行数和列数自己指定,
输入9,输出9*9口诀表,输出12,输出12*12的乘法口诀表。
#include <stdio.h>
#include <Windows.h>
1.0
int show_multiplication_tables(int n) {
	int i = 1;
	for (i; i <= n; i++) {
		int j = 1;
		for (j; j <= i; j++) {
			printf("%d*%d=%d ", i, j, i*j);
		}
		printf("\n");
	}
}

int main() {

	int n = 12;
	show_multiplication_tables(n);

	system("pause");
	return 0;
}

2.使用函数实现两个数的交换。
#include <stdio.h>
#include <Windows.h>

void exchange(int a, int b) {
	printf("a:%d b:%d\n", a, b);
	a = a + b;
	b = a - b;
	a = a - b;
	printf("a:%d b:%d\n", a, b);
}
int main() {
	exchange(10, 9);

	system("pause");
	return 0;
}

3.实现一个函数判断year是不是润年。
#include <stdio.h>
#include <Windows.h>

//3.实现一个函数判断year是不是润年。
void isLeapYear(int year) {
	if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
		printf("%d年是闰年\n", year);
	}
	else {
		printf("%d年不是闰年\n", year);
	}
}

int main() {
	int year;
	printf("请输入年份!\n");
	scanf_s("%d", &year);
	isLeapYear(year);

	system("pause");
	return 0;
}

4.创建一个数组,
实现函数init()初始化数组、
实现empty()清空数组、
实现reverse()函数完成数组元素的逆置。
要求:自己设计函数的参数,返回值。
#include <stdio.h>
#include <Windows.h>

#include <assert.h>

#define INT -1

void initArray(int arr[],int size) {
	assert(arr);
	int i = 0;
	for (i; i < size; i++) {
		arr[i] = INT;
	}
}

void emptyArray(int arr[], int size) {
	assert(arr);
	int i = 0;
	for (i; i < size; i++) {
		arr[i] = i;
	}
}

void reverseArray(int arr[], int size) {
	int start = 0;
	int end = size - 1;
	while (start < end) {
		arr[start] ^= arr[end];
		arr[end] ^= arr[start];
		arr[start] ^= arr[end];
		start++, end++;
	}
}
int main() {
	int arr[10];
	int size = sizeof(arr) / sizeof(arr[0]);
	initArray(arr, size);
	emptyArray(arr, size);
	reverseArray(arr, size);
	system("pause");
	return 0;
}

5.实现一个函数,判断一个数是不是素数。

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

//5.0 实现一个函数,判断一个数是不是素数。
int isPrime(int i){
	int j = 0;
	int temp = sqrt(i);
	for (j = 2; j < temp; j++) {
		if (i % j == 0){
			return 0;
		}
	}
	return 1;
}
int main()
{
	int i = 0;
	printf("请输入整数!\n");
	scanf_s("%d", &i);

	if (isPrime(i)){
		printf("%d是素数\n", i);
	}

	else{
		printf("%d不是素数\n", i);
	}

	system("pause");
	return 0;
}



猜你喜欢

转载自blog.csdn.net/qq_39026129/article/details/80215904