C++ : Calculate The Series (1) + (1+2) + … + (1+2+3+...+n)

Có thể bạn quan tâm

C++ Exercises: Calculate the series (1) + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+n)

Last update on November 20 2025 12:17:11 (UTC/GMT +8 hours)

13. Sum of the Series (1) + (1+2) + ... + (1+2+...+n)

Write a program in C++ to calculate the series (1) + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+n).

Visual Presentation:

C++ Exercises: Calculate the series (1) + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+n)

Sample Solution :-

C++ Code :

#include <iostream> // Including the input/output stream header file using namespace std; // Using the standard namespace to avoid writing std:: int main() // Start of the main function { int i, j, n, sum = 0, tsum; // Declaration of integer variables 'i', 'j', 'n', 'sum', and 'tsum' // Display a message to find the sum of the series (1) + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+n) cout << "\n\n Find the sum of the series (1) + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+n):\n"; cout << "------------------------------------------------------------------------------------------\n"; // Prompt the user to input the value for the nth term of the series cout << " Input the value for nth term: "; cin >> n; // Read the value entered by the user for (i = 1; i <= n; i++) // Outer loop to iterate from 1 to 'n' { tsum = 0; // Initializing 'tsum' to 0 for each iteration of the outer loop for (j = 1; j

Từ khóa » C++ 1 2