C 语言实例 - 计算一个数是否可为两个素数之和

C 语言教程 · 2019-02-20 22:13:35

计算一个数为两个素数之和。

实例

#include <stdio.h> int checkPrime(int n); int main() { int n, i, flag = 0; printf("输入正整数: "); scanf("%d", &n); for(i = 2; i <= n/2; ++i) { // 检测判断 if (checkPrime(i) == 1) { if (checkPrime(n-i) == 1) { printf("%d = %d + %d\n", n, i, n - i); flag = 1; } } } if (flag == 0) printf("%d 不能分解为两个素数。", n); return 0; } // 判断素数 int checkPrime(int n) { int i, isPrime = 1; for(i = 2; i <= n/2; ++i) { if(n % i == 0) { isPrime = 0; break; } } return isPrime; }

输出结果为:

输入正整数: 34
34 = 3 + 31
34 = 5 + 29
34 = 11 + 23
34 = 17 + 17

点击查看所有 C 语言教程 文章: https://codercto.com/courses/l/17.html

查看所有标签

Powerful

Powerful

Patty McCord / Missionday / 2018-1-25

Named by The Washington Post as one of the 11 Leadership Books to Read in 2018 When it comes to recruiting, motivating, and creating great teams, Patty McCord says most companies have it all wrong. Mc......一起来看看 《Powerful》 这本书的介绍吧!

SHA 加密
SHA 加密

SHA 加密工具

html转js在线工具
html转js在线工具

html转js在线工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具