C ++ project: Answers to questions about small test cycles

Source: Internet
Author: User

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 <   

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.