代码练习_3_25_p1

我之所以这么努力,
是不想在年华老去之后鄙视自己.

//1.打印100~200 之间的素数 
//解法一
#include <stdio.h>
#include <stdlib.h>
int main(){
	int i = 0;
	int count = 0;
	for (i = 100; i <= 200; i++){
		int j = 0;
		for (j = 2; j <= i - 1; j++){
			if (i%j == 0)
				break;
		}
		if (i == j){
			count++;
			printf("%d ", i);
		}
     }
	printf("\ncount = %d\n", count);
	system("pause");
	return 0;
}
//解法二
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(){
	int i = 0;
	int count = 0;
	for (i = 101; i <= 200; i += 2){
		int j = 0;
		for (j = 2; j <= sqrt(i); j++){
			if (i%j == 0)
				break;
		}
		if (j > sqrt(i)){
			count++;
			printf("%d ", i);
		}
	}
	printf("\ncount = %d\n", count);
	system("pause");
	return 0;
}
//2. 输出乘法口诀表 
#include <stdio.h>
#include <math.h>
int main(){
	int i = 0;
	int j = 0;
	for (i = 1; i <= 9; i++){
		for (j = 1; j <= i; j++){
			printf("%d*%d=%2d ", i, j, i*j);
		}
		printf("\n");
	}
	system("pause");
	return 0;
}
//3. 判断1000年---2000年之间的闰年
#include <stdio.h>
#include <math.h>
int main(){
	int year = 0;
	int count = 0;
	for (year = 1000; year <= 2000; year++){
		if ((year % 400 == 0) || ((year % 4 == 0) &&
			(year % 100 != 0))){
			count++;
			printf("%d ", year);
		}
	}
	printf("count=%d\n", count);
	system("pause");
	return 0;
}
//4.判定一个数是否为奇数
#include <stdio.h>
int IsOdd(int x){
	if (x % 2 == 0){
		return 0;
	}
	return 1;
}
int main(){
	printf("%d\n", IsOdd(-101));
	system("pause");
	return 0;
}
//5.输出1-100之间的奇数
#include <stdio.h>
int IsOdd(int x){
	if (x % 2 == 0){
		return 0;
	}
	return 1;
}
int main(){
	int num = 1;
	while (num <= 100){
		if (IsOdd(num) == 1){
			printf("%d\n", num);
		}
		num += 1;
	}
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_44770155/article/details/89236962