Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C++ Exercises: Calculate the sum of the series 1.2+2.3+3.4+4.5+5.6+.....

C++ For Loop: Exercise-67 with Solution

Write a program in C++ to calculate the sum of the series 1.2+2.3+3.4+4.5+5.6+.......

Sample Solution:-

C++ Code :

#include <iostream>
using namespace std;
int main()
{
    int trm;
    double num, sum, i, m;
    cout << "\n\n calculate the sum of the series 1.2+2.3+3.4+4.5+5.6+......:\n";
    cout << "----------------------------------------------------------------\n";
    cout << " Input the last integer between 1 to 98 without fraction you want to add: ";
    cin >> trm;
    for (i = 1; i <= trm; i++) 
    {
        if (i < 9) 
        {
            m = .1;
        }
        else 
        {
            m = .01;
        }
        num = i + ((i + 1) * (m));
        sum = sum + num;
        cout << num;
        if (i < trm) 
        {
            cout << " + ";
        }
    }
    cout << "\n The sum of the series =" << sum << endl;
}

Sample Output:

 calculate the sum of the series 1·2+2·3+3·4+4.5+5.6+......:           
----------------------------------------------------------------       
 Input the last integer between 1 to 98 without fraction you want to ad
d: 10                                                                  
1.2 + 2.3 + 3.4 + 4.5 + 5.6 + 6.7 + 7.8 + 8.9 + 9.1 + 10.11            
 The sum of the series =59.61

Flowchart:

Flowchart: Calculate the sum of the series 1.2+2.3+3.4+4.5+5.6+.....

C++ Code Editor:

Contribute your code and comments through Disqus.

Previous: Write code to create a checkerboard pattern with the words "black" and "white".
Next: Write a program that will print the first N numbers for a specific base.

What is the difficulty level of this exercise?