1、打印100-200之间的素数。
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i = 0;
for (i = 100; i <= 200; i++)
{
int j = 0;
for (j = 2; j <= i - 1; j++)
{
if (i%j == 0)
{
break;
}
}
if (j >= i)
{
printf("%d", i);
system("pause");
}
}
return 0;
}
2、输出乘法口诀表。
#include<stdio.h>
#include<stdlib.h>
main()
{
int i=0, j=0;
for (i = 1; i <= 9; i++)//循环计算1~9
{
for (j = 1; j <= i; j++)//输出数i的i个乘法项
{
printf("%d*%d=%d ", i, j, i*j);
}
printf("\n");//输出换行符
system("pause");
}
}
3、输出1000年-2000年之间的闰年。
#include<stdio.h>
#include<stdlib.h>
int main()
{
int year;
for (year = 1000; year <= 2000; year++)
{
if (year % 400 == 0)//世纪年能整除400的是闰年。
{
printf("%d ", year);
}
else if (year % 4 == 0 && year % 100 != 0)//普通年能整除4且不能整除100的为闰年。
{
printf("%d ", year);
}
}
system("pause");
return 0;
}
4、给定两个整形变量的值,将两个值的内容进行交换。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a = 10;
int b = 20;
int c = 0;
printf(“a = %d b = %d\n”, a, b);
c = a;
a = b;
b = c;
printf(“a = %d b = %d\n”, a, b);
system(“pause”);
return 0;
}
5、不允许创建临时变量,交换两个数的内容。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a = 10;
int b = 20;
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);
system(“pause”);
return 0;
}
6、求10 个整数中最大值。
#include<stdio.h>
#include<stdlib.h>
int main()
{
int arr[10] = { 0 };
int i = 0;
int max = 0;
printf(“请输入十个整数:”);
for (i = 0; i < 10; i++)
{
scanf_s("%d", &arr[i]);
max = arr[0];
}
for (i = 0; i < 10; i++)
{
if (arr[i]>max)
{
max = arr[i];
}
}
printf(“十个数中的最大值为:%d\n”, max);
system(“pause”);
return 0;
}
7、将三个数按从大到小输出。
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a, b, c, t;
scanf("%d %d %d", &a, &b, &c);
if (a < b)
{
t = a;
a = b;
b = t;
}
if (a < c)
{
t = a;
a = c;
c = t;
}
if (b < c)
{
t = b;
b = c;
c = t;
}
printf("%d %d %d", a, b, c);
system(“pause”);
return 0;
}
8、求两个数的最大公约数。
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a = 0, b = 0;
printf(“请任意输入两个数:”);
scanf("%d%d", &a, &b);
if (a >= b)
{
int i = 0;
for (i = b; i >= 1; i--)
{
if ((a%i) == 0 && (b%i) == 0)
{
printf(“最大公约数为:%d\n”, i);
break;
}
}
}
else
{
int j = 0;
for (j = a; j>0; j–)
{
if ((a%j) == 0 && (b%j) == 0)
{
printf(“最大公约数为:%d\n”, j);
break;
}
}
}
system(“pause”);
return 0;
}
9、将数组A中的内容和数组B中的内容进行交换。
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
int main()
{
int arr1[] = { 1, 3, 5, 7, 9 };
int arr2[] = { 3, 4, 5, 6 ,7 };
int tmp;
int i = 0;
for (i = 0; i < sizeof(arr1) / sizeof(arr1[0]); i++)
{
tmp = arr1[i];
arr1[i] = arr2[i];
arr2[i] = tmp;
}
for (i = 0; i < sizeof(arr1) / sizeof(arr1[0]); i++)
{
printf("%d ", arr1[i]);
}
printf("\n");
for (i = 0; i < sizeof(arr1) / sizeof(arr1[0]); i++)
{
printf("%d ", arr2[i]);
}
printf("\n");
system("pause");
return 0;
}
10、计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值。
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
int i;
float sum = 0.0, temp = 0.0;
for (i = 1; i <= 100; i++)
{
temp = pow(-1, i + 1);
sum = sum + temp * 1 / i;
}
printf("sum is %f\n", sum);
system("pause");
return 0;
}
11、编写程序数一下 1到 100 的所有整数中出现多少次数字9。
#include <stdio.h>
#include<stdlib.h>
int main() {
int i = 0;
int k = 0;
for (i = 1; i <= 100; i++)
{
if (i % 10 == 9)
{
k++;
}
if (i / 10 == 9)
{
k++;
}
}
printf("%d\n", k);
system("pause");
return 0;
}
12、在屏幕上输出以下图案:
#include<stdio.h>
#include<stdlib.h>
int main()
{
for (int x = 1; x<8; x++)
{
for (int y = 1; y <= 2 * x - 1; y++)
{
printf("*");
}
printf("\n");
}
for (int x = 6; x>0; x--)
{
for (int y = 1; y <= 2 * x - 1; y++)
{
printf("*");
}
printf("\n");
}
system("pause");
return 0;
}
13、求出0~999之间的所有“水仙花数”并输出。
“水仙花数”是指一个三位数,其各位数字的立方和确好等于该数本身,如;153=1+5+3?,则153是一个“水仙花数”。
在数论中,水仙花数(Narcissistic number)也称为自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数(Armstrong number),是指一N位数,其各个数之N次方和等于该数。
例如153、370、371及407就是三位数的水仙花数,其各个数之立方和等于该数:
153 = 1^3 + 5^3 + 3^3。
370 = 3^3 + 7^3 + 0^3。
371 = 3^3 + 7^3 + 1^3。
407 = 4^3 + 0^3 + 7^3。
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
int i = 0;
for (i = 0; i < 1000; i++)
{ //计算位数
int count = 1;
int tmp = i;
int sum = 0;
while (tmp / 10)
{
count++;
tmp = tmp / 10;
} //计算次方和
tmp = i;
while (tmp)
{
sum += pow(tmp % 10, count);
tmp = tmp / 10;
} //判断
if (sum == i)
printf("%d ", i);
}
system("pause");
return 0;
}
14、求Sn=a+aa+aaa+aaaa+aaaaa的前5项之和,其中a是一个数字,
例如:2+22+222+2222+22222
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include<stdlib.h>
int main()
{
int i = 0;
int a = 0;
int sum = 0;
int num = 0;
scanf("%d", &num);
for (i = 0; i < 5; i++)
{
a = a * 10 + num;
sum = sum + a;
}
printf("%d'n", sum);
system("pause");
return 0;
}