This answer quotes ChatGPT
_ Please reply me if you have questions
1, C++ program: input a positive integer, output in reverse order.
#include
using namespace std;
int main() {
int num, reverse = 0;
cout << "请输入一个正整数:";
cin >> num;
while(num > 0) {
int digit = num % 10;
reverse = reverse * 10 + digit;
num /= 10;
}
cout << "倒序输出为:" << reverse << endl;
return 0;
}
2, C++ program: input a positive integer N, output from 2 to N all the prime sum.
#include
using namespace std;
bool isPrime(int num) {
if(num == 1) {
return false;
}
for(int i = 2; i <= num/2; i++) {
if(num % i == 0) {
return false;
}
}
return true;
}
int main() {
int n;
int sum = 0;
cout << "请输入一个正整数:";
cin >> n;
for(int i = 2; i <= n; i++) {
if(isPrime(i)) {
sum += i;
}
}
cout << "2到" << n << "的素数和为:" << sum << endl;
return 0;
}
In the above code, the isPrime function is used to determine whether a number is prime, returning true if it is prime, false otherwise. In the main function, we use a loop to determine in turn whether all the numbers from 2 to N are prime, and if so, add them to sum. Finally output the result.