源码:
# include <stdio.h>
int main()
{
int x, y, num1, num2, temp;
printf("请输入两个正整数:\n");
scanf("%d %d", &num1, &num2);
if(num1 < num2)
{
temp = num1;
num1 = num2;
num2 = temp;
}
x = num1;
y = num2;
while(y != 0)
{
temp = x%y;
x = y;
y = temp;
}
printf("它们的最大公约数为:%d\n", x);
printf("它们的最小公倍数为:%d\n", num1*num2/x);
return 0;
}
......
源码:
# include <math.h>
# include <stdio.h> /* 数学函数库 */
int main()
{
/* 用s表示多项式的值,用t表示每一项的值 */
double s, t, x; // 此处用双精度声明变量
int n;
printf("please input x: ");
scanf("%lf", &x); // %lf 代表用双精度输入
/* 赋初值 */
t = x;
n = 1;
s = x;
/* 进行叠加运算 */
do
{
n = n + 2 ;
t = t * (-x*x)/((float)(n)-1)/(float)(n);
s = s + t;
} while (fabs(t)>=1e-8); // 本来是要大于0,但也可大于一个很小的数即可
printf("sin(%f) = %lf\n", x, s);
return 0;
}
......
源码:
# include <stdio.h>
int main( )
{
int radius;
double area;
for(radius = 1; radius <= 10 ; radius++)
{
area = 3.1416 * radius * radius;
/* 若圆面积超过120,则跳出for循环,不予输出 */
if(area >= 120.0)
break; // 跳出循环
printf("square = %f\n", area);
}
printf("now radius=%d\n\n", radius-1);
for(radius = 1; radius <= 10 ; radius++)
{
area = 3.1416 * radius * radius;
/* 若圆面积没有超过60,则不输出而是从新开始循环 */
if(area < 120.0 ......
源码:
# include <stdlib.h>
# include <stdio.h>
int main()
{
int month;
int day;
printf("please input the month number: ");
scanf("%d", &month);
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: day=31; // 有31天的月份情况
break;
case 4:
case 6:
case 9:
case 11: day=30; // 有30天的月份的情况
break;
case 2: day=28; // 非闰年的2月有28天
&nbs ......
源码:
/* 使用选择法排序 */
# include <stdio.h>
int main()
{
int i, j, min, temp;
/* 定义一个整型的一维数组 */
int array[10];
/* 输入数据 */
printf("Please input ten integer: \n");
for(i=0; i<10; i++)
{
printf("array[%d] = ", i);
scanf("%d", &array[i]);
}
printf("The array is: ");
for(i=0; i<10; i++)
printf("%d ", array[i]);
printf("\n");
/* 排序 */
for(i=0; i<9; i++) //顺着0到最后一个元素进行选择排序(递增)
{
min = i;
for( ......
源码:
# include <stdio.h>
int main()
{
int array[16][16];
int i, j, k, m, n;
/* 变量初始化 */
m = 1;
while(m == 1)
{
printf("请输入n(0<n<=15且为奇数):");
scanf("%d", &n);
/* 判断n是否是大于0小于等于15的奇数 */
if((n!=0) && (n<=15) && (n%2!=0))
{
printf("矩阵阶数是 %d\n", n);
m = 0; // 制造循环输入机制,直到输入正确方可退出循环
}
}
/* 数组赋初值为0 */
......