1,
#include<stdio.h> int Prime(int n); int main() { int n; int i; int sum = 0; printf("Enter an integer:"); scanf_s("%d", &n); for (i = 0; i < n; i++) if (Prime(i)) sum += i; printf("Sum of prime from 1 to %d is: %d", n, sum); return 0; } int Prime(int n) { int d; if (n == 0 || n == 1) return 0; for (d = 2; d < n / 2 + 1; d++) if (n % d == 0) return 0; return 1; }
2,
#include<stdio.h> int Prime(int n); int main() { int n; int num[100]; int i_num = 0; printf("Enter an even number:"); scanf_s("%d", &n); for (int i = 0; i < n; i++) if (Prime(i)) if (Prime(n - i) ) num[i_num++] = i; for (int i = 0; i < i_num; i++) printf("%d=%d+%d\n", n, num[i], n - num[i]); return 0; } int Prime(int n) { int d; if (n == 0 || n == 1) return 0; for (d = 2; d < n / 2 + 1; d++) if (n % d == 0) return 0; return 1; }
3,
#include<stdio.h> int Sum(int n) { printf("sizeof(long long): %d\n", sizeof(long long)); int sum = 0; while (n != 0) { sum += n % 10; n /= 10; } return sum; } int main() { int mima=0; int three = 1; int n; int sum = 0; printf("设置密码:"); scanf_s("%d", &mima); printf("输入密码(最多三次):"); scanf_s("%d", &n); while (three < 4) { if (n == mima) { printf("欢迎使用!\n"); sum = Sum(mima); printf("密码各位之和:%d", sum); break; } if (three < 3) printf("输入错误,请重新输入!"); else { printf("非法用户!\n"); break; } three++; scanf_s("%d", &n); } return 0; }
4,太简单不写了,注意是有条件的调用,不然一直调用直接卡死