用c编写的一些程序

**1.打印100~200 之间的素数 **
判断一个数是否为一个素数,我调用<math.h>中的sqrt(x)的写法,正是因为一个数的因数是成对出现的,所以开方后就只需要判断它的前一半可以减少一些循环次数。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(){
	int count = 1,i = 2;
	printf("输出100到200之间的素数:\n");
	for (int num = 100; num <= 200; num++){
		for ( i = 2; i <= sqrt(num); i++){
			if (num%i == 0)break;
		}
		if (i > sqrt(num)){
			printf("%d ", num);
			if (count++ == 10){
				printf("\n");  //记录每输出十个数换一行
			}
		}
	}
	system("pause");
	return 0;
}

运行结果:
输出100到200之间的素数:
101 103 107 109 113 127 131 137 139 149
151 157 163 167 173 179 181 191 193 197 199 请按任意键继续. . .
**2.输出乘法口诀表 **
显示乘法口诀表时,由于乘法表的1乘9与9乘1是同一个意思,所以可以沿对角线显示出一个三角形,第一个因子永远大于或等于第二个因子。

#include <stdio.h>
#include <stdlib.h>
int main(){
	printf("输出一张乘法口诀表:\n");
	int i = 1, j = 1;
	for (i = 1; i <= 9; i++){                     //第一个因子
		for (j = 1; j <= i; j++){                 //第二个因子
			printf("%d * %d = %2d  ",i, j, i*j);//%2d可以控制printf十进制位数
		}
		printf("\n");
	}
	system("pause");
	return 0;
}

运行结果:
输出一张乘法口诀表:
1 * 1 = 1
2 * 1 = 2 2 * 2 = 4
3 * 1 = 3 3 * 2 = 6 3 * 3 = 9
4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16
5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25
6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36
7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49
8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64
9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81
请按任意键继续. . .

**3.判断1000年—2000年之间的闰年 **
闰年是可以被四和四百整除但是不能被100整除的年份,除了被400整除的年份可以直接输出,其余的都要进行能不能对四整除和一百整除进行判断,所以可以将“和”写为&&,“除了“写为||。

#include <stdio.h>
#include <stdlib.h>
int main(){
	printf("判断1000到2000年之间的闰年:\n");
	int i = 1, leap = 1000;//leap year 闰年
	for (; leap <= 2000; leap++){
		if (leap%4==0&&leap%100!=0||leap%400==0){           //“和”以及“或”
				printf("%d ", leap);
			if (i++ % 10 == 0){
				printf("\n"); //每十个换一行
			}
		}
	}
	printf("\n");//最后一个换行
	system("pause");
	return 0;
}

运行结果:
判断1000到2000年之间的闰年:
1004 1008 1012 1016 1020 1024 1028 1032 1036 1040
1044 1048 1052 1056 1060 1064 1068 1072 1076 1080
1084 1088 1092 1096 1104 1108 1112 1116 1120 1124
1128 1132 1136 1140 1144 1148 1152 1156 1160 1164
1168 1172 1176 1180 1184 1188 1192 1196 1200 1204
1208 1212 1216 1220 1224 1228 1232 1236 1240 1244
1248 1252 1256 1260 1264 1268 1272 1276 1280 1284
1288 1292 1296 1304 1308 1312 1316 1320 1324 1328
1332 1336 1340 1344 1348 1352 1356 1360 1364 1368
1372 1376 1380 1384 1388 1392 1396 1404 1408 1412
1416 1420 1424 1428 1432 1436 1440 1444 1448 1452
1456 1460 1464 1468 1472 1476 1480 1484 1488 1492
1496 1504 1508 1512 1516 1520 1524 1528 1532 1536
1540 1544 1548 1552 1556 1560 1564 1568 1572 1576
1580 1584 1588 1592 1596 1600 1604 1608 1612 1616
1620 1624 1628 1632 1636 1640 1644 1648 1652 1656
1660 1664 1668 1672 1676 1680 1684 1688 1692 1696
1704 1708 1712 1716 1720 1724 1728 1732 1736 1740
1744 1748 1752 1756 1760 1764 1768 1772 1776 1780
1784 1788 1792 1796 1804 1808 1812 1816 1820 1824
1828 1832 1836 1840 1844 1848 1852 1856 1860 1864
1868 1872 1876 1880 1884 1888 1892 1896 1904 1908
1912 1916 1920 1924 1928 1932 1936 1940 1944 1948
1952 1956 1960 1964 1968 1972 1976 1980 1984 1988
1992 1996 2000
请按任意键继续. . .

猜你喜欢

转载自blog.csdn.net/zjgyjd/article/details/82807990