Print function by multiplication table / exchange of two numbers / leap year judging / create an array / Analyzing primes

1. To achieve a function, print multiplication tables, rows and columns of the table formulas own designated,
input 9, the output 9 9 Table formulas, 12 input, 12 output the multiplication tables 12.

#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>
#include<stdlib.h>
void Multiplication_Table(int num)
{
	for (int i = 1; i <= num; i++)
	{
		for (int j = 1; j <= i; j++)
		{
		
			printf("%d*%d=%d ", i, j, i*j); 
		}
		printf("\n");
	}

}
int main()
{
	int num=0;
	printf("请分别输入一个数,求其乘法口诀\n");
	scanf("%d", &num);
	Multiplication_Table(num);
	system("pause");
	return 0;
}

2. functions for the exchange of two numbers.
// parameter is a temporary copy of the argument
// changes to the parameter does not change the argument
// only address the arguments passed to the parameter of parameter changes will be passed to the argument

void Swap(int i,int j)
{
	int tmp = i;
	i =j;
	j = tmp;

}
int main()
{
	int i, j;
	printf("请输入分别两个数字:\n");
	scanf("%d,%d", &i, &j);
	printf("a=%d b=%d\n", i, j);
	Swap(i, j);
	printf("a=%d b=%d\n", i, j);
	system("pause");
	return 0;

}

Here Insert Picture Description

#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>
#include<stdlib.h>
void Swap(int* i,int* j)
{
	int tmp = *i;
	*i =*j;
	*j = tmp;

}
int main()
{
	int i, j;
	printf("请输入分别两个数字:\n");
	scanf("%d,%d", &i, &j);
	printf("a=%d b=%d\n", i, j);
	Swap(&i, &j);
	printf("a=%d b=%d\n", i, j);
	system("pause");
	return 0;

}

Here Insert Picture Description

3. implement a function to determine year is not a leap year.

#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>
#include<stdlib.h>
//判断是否为闰年
int Leap_Year(int year)
{
	if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
	{
		return 1;
	}
	else
	{
		return 0;
	}

}
int main()
{
	int year;
	printf("请输入一个年份:\n");
	scanf("%d", &year);
	int ret = Leap_Year(year);
	if (ret == 1)
	{
		printf("%d年是闰年\n", year);
	}
	else if (ret == 0)
	{
		printf("%d年不是闰年\n", year);
	}
	system("pause");
	return 0;
}

Create an array,
to achieve the function init () to initialize an array,
implement empty () empty array,
to achieve reverse () function to complete the array elements of Retrograde.
Requirements: own design function parameters, return values.

#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>
#include<stdlib.h>
//初始化
void init(int arr[])
{
	for (int i = 0; i < 6; i++)
	{
		arr[i] = i;
	}
}
//逆置
void reverse(int arr[])
{
	for (int i = 0; i < 3; i++)
	{
		int temp = arr[i];
		arr[i] = arr[5 - i];
		arr[5 - i] = temp;
	}
}
//清空
void empty(int arr[])
{
	for (int i = 0; i < 6; i++)
		arr[i] = 0;
}
//打印数组
void print(int arr[])
{
	for (int i = 0; i < 6; i++)
	{
		printf("%d ", arr[i]);
	}
	printf("\n");
}
int main()
{
	int arr[6] = { 0 };
	init(arr);
	print(arr);
	reverse(arr);;
	print(arr);
	empty(arr);
	print(arr);
	system("pause");
	return 0;
}

The realization of a function, determining a number is not a prime number

#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
//判断是否为素数
int isPrime(int x)
{
	for (int i = 2; i <=sqrt(x); i++)
	{
		if (x%i == 0)
		{
			return 0;
		}
	}
	return 1;
}
int main()
{
	int prime;
	printf("请输入一个数字:\n");
	scanf("%d", &prime);
	if (isPrime(prime)==1)
	{
		printf("这个数为素数\n");
	}
	else
	{
		printf("这个数不是素数\n");
	}
	system("pause");
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_44936160/article/details/90146598