C ++ project: Answers to questions about small test cycles
Project-test cycle]
Write out the program for solving the following task. [Note: m is a variable, input in the program]
(1) Calculate the sum of squares from 1 to m.
(2) Calculate the sum of all the odd numbers between 1 and m.
(3) Calculate the reciprocal sum of 1 to m, that is 1 + 12 + 13 + 14 +... + 1 m
(4) Evaluate: 1? 12 + 13? 14 +... + (? 1) (m + 1) × 1 m
(5) m !, That is 1 × 2 × 3 ×... × m
[Reference]
Write out the program for solving the following task. [Note: m is a variable, input in the program]
(1) Calculate the sum of squares from 1 to m.
#include
using namespace std;int main( ){ int n,m,total; cin>>m; n=1; total=0; while(n<=m) { total+=(n*n); n++; } cout<<"total="<
Or use the for Loop:
#include using namespace std;int main( ){ int n,m,total; cin>>m; total=0; for(n=1;n<=m;n++) { total+=(n*n); } cout<<"total="<
(2) Calculate the sum of all the odd numbers between 1 and m.
#include using namespace std;int main( ){ int n,m,total; cin>>m; n=1; total=0; while(n<=m) { total+=n; n+=2; } cout<<"total="<
Or use the for Loop:
#include using namespace std;int main( ){ int n,m,total; cin>>m; total=0; for(n=1;n<=m;n+=2) { total+=n; } cout<<"total="<
(3) Calculate the reciprocal sum of 1 to m, that is 1 + 12 + 13 + 14 +... + 1 m
# Include Using namespace std; int main () {int n, m; double total; cin> m; n = 1; total = 0; while (n <= m) {total + = (1.0/n); // pay attention to the type conversion caused by 1.0, which is very important! N ++;} cout <"total =" <
Or use the for Loop:
# Include Using namespace std; int main () {int n, m; double total; cin> m; n = 1; total = 0; for (n = 1; n <= m; n ++) {total + = (1.0/n); // pay attention to the type conversion caused by 1.0, which is very important! } Cout <"total =" <
(4) Evaluate: 1? 12 + 13? 14 +... + (? 1) (m + 1) × 1 m
# Include Using namespace std; int main () {int n, m, sign; double total; cin> m; n = 1; total = 0; sign = 1; // use sign to indicate the sign of the accumulated item. This is a technique for processing Positive and Negative addition while (n <= m) {total + = (sign * (1.0/n )); n ++; sign * =-1; // sign variable} cout <"total =" <
Or use the for Loop:
# Include Using namespace std; int main () {int n, m, sign; double total; cin> m; n = 1; sign = 1; // use sign to indicate the sign of the accumulated item. This is the technique for handling positive and negative addition. total = 0; for (n = 1; n <= m; n ++) {total + = (sign * (1.0/n); // pay attention to the type conversion caused by 1.0, which is very important! Sign * =-1; // sign variable} cout <"total =" <
(5) m !, That is 1 × 2 × 3 ×... × m
# Include Using namespace std; int main () {int n, m; long fact; // The factorial value is large. For data types, consider some cin> m; n = 1; fact = 1; while (n <= m) {fact * = n; n ++;} cout < # Include Using namespace std; int main () {int n, m; long fact; // The factorial value is large. For data types, consider some cin> m; fact = 1; for (n = 1; n <= m; n ++) {fact * = n;} cout <