C language daily small exercises (building wheels)-1.6

1. Implement a function, print the multiplication formula table, specify the number of rows and columns of the formula table by yourself,
input 9, output a 9*9 formula table, output 12, and output a 12*12 multiplication formula table.
#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. Use a function to exchange two numbers.
#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. Implement a function to judge whether the year is a good year.
#include <stdio.h>
#include <Windows.h>

//3. Implement a function to determine whether the year is a good year.
void isLeapYear(int year) {
	if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
		printf("%d is a leap year\n", year);
	}
	else {
		printf("%d year is not a leap year\n", year);
	}
}

int main() {
	int year;
	printf("Please enter the year!\n");
	scanf_s("%d", &year);
	isLeapYear(year);

	system("pause");
	return 0;
}

4. Create an array,
implement the function init() to initialize the array,
implement empty() to clear the array, and
implement the reverse() function to complete the inversion of the array elements.
Requirements: Design the parameters of the function yourself and return the value.
#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. Implement a function to determine whether a number is prime or not.

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

//5.0 Implement a function to determine whether a number is prime.
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;
}
intmain()
{
	int i = 0;
	printf("Please enter an integer!\n");
	scanf_s("%d", &i);

	if (isPrime(i)){
		printf("%d is a prime number\n", i);
	}

	else{
		printf("%d is not prime\n", i);
	}

	system("pause");
	return 0;
}



Guess you like

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