1.调用函数实现俩数的交换
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
void swap(int* px, int* py)
{
int tmp = 0;
tmp = *px;
*px = *py;
*py = tmp;
}
int main()
{
int x = 15;
int y = 25;
printf("x = % d y = % d\n", x, y);
swap(&x, &y);
printf("x = % d y = % d\n", x, y);
system("pause");
return 0;
}
2.调用函数实现输出自定义的乘法口诀表
#define _CRT_SECURE_NO_WARNINGS 1
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
void koujuebiao(int x,int y)
{
int i, j;
for (i = 1; i<= x; i++)
{
for (j = 1; j<= y; j++)
printf("%d*%d=%d\t ", i, j, i*j);
printf("\n");
}
}
int main()
{
int i = 0;
int j = 0;
printf("请输入口诀表的行数和列数");
scanf("%d", &i);
j = i;
koujuebiao(i, j);
getchar();
system("pause");
return 0;
}
3.调用函数判断某年是否为闰年:
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include <math.h>
int is_leap_year(int year)
{
return(year % 4 == 0 && year % 100 != 0 || year % 400 == 0);
}
int main()
{
int year = 0;
for (year = 1000; year <= 2000; year++)
{
if (is_leap_year(year))
printf("%d\n", year);
}
system("pause");
return 0;
}
4.调用函数判断一个数是否为素数
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
int is_prime(int i)
{
int j = 0;
for (j = 2; j < 100; j++)
{
if (i%j == 0)
{
return 0;
}
}
return 1;
}
int main()
{
int i = 0;
for (i = 101; i <= 200; i += 2)
{
if (is_prime(i) )
{
printf("%d\n", i);
}
}
system("pause");
return 0;
}