
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Sum of the series 1^1 + 2^2 + 3^3 + ... + n^n using recursion in C++
In this problem, we are given a number n which defines the nth terms of the series 1^1 + 2^2 + 3^3 + … + n^n. Our task is to create a program that will find the sum of the series.
Let’s take an example to understand the problem,
Input
n = 4
Output
30
Explanation −sum = (1^1) + (2^2) + (3^3) + (4^4) = 1 + 4 + 9 + 16 = 30.
To solve this problem, we will loop from 1 to n. Find the square of each number. And add each to the sum variable.
Algorithm
Initialize sum = 0 Step 1: Iterate from i = 1 to n. And follow : Step 1.1: Update sum, sum += i*i Step 2: Print sum.
Example
Program to illustrate the working of our solution,
#include <iostream> using namespace std; long long calcSeriesSum(int n) { long long sum = 0; for( int i = 1; i <= n; i++ ) sum += (i*i); return sum; } int main() { int n = 7; cout<<"Sum of the series 1^1 + 2^2 + 3^3 + ... + "<<n<<"^"<<n<<" is "<<calcSeriesSum(n); return 0; }
Output
Sum of the series 1^1 + 2^2 + 3^3 + ... + 7^7 is 140
- Related Articles
- Plus One in Python
- Sum of the series 1 + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+n) in C++
- C++ program to find the sum of the series 1 + 1/2^2 + 1/3^3 + …..+ 1/n^n
- Sum of series 1^2 + 3^2 + 5^2 + . . . + (2*n – 1)^2
- LocalDateTime plus() method in Java
- Duration plus() method in Java
- LocalTime plus() method in Java
- Instant plus() method in Java
- LocalDate plus() method in Java
- Sum of the series 1 / 1 + (1 + 2) / (1 * 2) + (1 + 2 + 3) / (1 * 2 * 3) + … + upto n terms in C++
- Sum of the Series 1 + x/1 + x^2/2 + x^3/3 + .. + x^n/n in C++
- Examine whether root 2 plus 2 square is rational or irrational
- Sum of the Series 1/(1*2) + 1/(2*3) + 1/(3*4) + 1/(4*5) + ... in C++\n
- Sum of series 1^2 + 3^2 + 5^2 + . . . + (2*n - 1)^2 in C++
- C++ program to find the sum of the series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!

Advertisements