In the previous article, we have discussed Python Program to Convert Octal to Hexadecimal
Given a number N the task is to find the sum of the given series 1^1+2^2+3^3+…..+N^N for the given number in Python.
Examples:
Example1:
Input:
Given Number =3
Output:
The sum of the series till the given number { 3 } is : 32
- Python Program to Find Sum of Series 5^2 + 10^2 + 15^2 +…..N^2
- Python Program to Find Sum of Series 1^1/1+2^2/2+3^3/3…+n^n/n
- Python Program to Find Sum of Series 1^1/1!+2^2/2!+3^3/3!…+n^n/n!
Example2:
Input:
Given Number =6
Output:
The sum of the series till the given number { 6 } is : 50069
Program to find the Sum of Series 1^1+2^2+3^3…+N^N in Python
Below are the ways to find the sum of the given series 1^1+2^2+3^3+…..+N^N for the given number in Python:
- Using For loop and pow() function (Static Input)
- Using For loop and pow() function (User Input)
- Using For loop and ** operator (Static Input)
- Using For loop and ** operator (User Input)
Method #1: Using For loop and pow() function (Static Input)
Approach:
- Give the number N as static input and store it in a variable.
- Take a variable say resltsum which gives the sum of the given series till N and initialize its value to 0.
- Loop from 1 to given number using the For loop.
- Inside the For loop, Calculate the value of iterator value ^ iterator value using the pow function and store it in a variable.
- Add the above variable to the resltsum .
- Print the resltsum value which is the result of the series till the given Number N.
- The Exit of the Program.
Below is the implementation:
# Give the number N as static input and store it in a variable. gvnNumbr = 3 # Take a variable say resltsum which gives the sum of # the given series till N and initialize its value to 0. resltsum = 0 # Loop from 1 to given number using the For loop. for k in range(1, gvnNumbr+1): # Inside the For loop, Calculate the value of iterator value ^ iterator value # using the pow function and store it in a variable. powervl = pow(k, k) # Add the above variable to the resltsum resltsum = resltsum+powervl # Print the resltsum value which is the result of the series till the given Number N. print( 'The sum of the series till the given number {', gvnNumbr, '} is :', resltsum)
Output:
The sum of the series till the given number { 3 } is : 32
Method #2: Using For loop and pow() function (User Input)
Approach:
- Give the number N as user input using the int(input()) function and store it in a variable.
- Take a variable say resltsum which gives the sum of the given series till N and initialize its value to 0.
- Loop from 1 to given number using the For loop.
- Inside the For loop, Calculate the value of iterator value ^ iterator value using the pow function and store it in a variable.
- Add the above variable to the resltsum .
- Print the resltsum value which is the result of the series till the given Number N.
- The Exit of the Program.
Below is the implementation:
# Give the number N as user input using the int(input()) function # and store it in a variable. gvnNumbr = int(input('Enter some random number N = ')) # Take a variable say resltsum which gives the sum of # the given series till N and initialize its value to 0. resltsum = 0 # Loop from 1 to given number using the For loop. for k in range(1, gvnNumbr+1): # Inside the For loop, Calculate the value of iterator value ^ iterator value # using the pow function and store it in a variable. powervl = pow(k, k) # Add the above variable to the resltsum resltsum = resltsum+powervl # Print the resltsum value which is the result of the series till the given Number N. print( 'The sum of the series till the given number {', gvnNumbr, '} is :', resltsum)
Output:
Enter some random number N = 7 The sum of the series till the given number { 7 } is : 873612
Method #3: Using For loop and ** operator (Static Input)
Approach:
- Give the number N as static input and store it in a variable.
- Take a variable say resltsum which gives the sum of the given series till N and initialize its value to 0.
- Loop from 1 to given number using the For loop.
- Inside the For loop, Calculate the value of iterator value ^ iterator value using the ** operator and store it in a variable.
- Add the above variable to the resltsum .
- Print the resltsum value which is the result of the series till the given Number N.
- The Exit of the Program.
Below is the implementation:
# Give the number N as static input and store it in a variable. gvnNumbr = 5 # Take a variable say resltsum which gives the sum of # the given series till N and initialize its value to 0. resltsum = 0 # Loop from 1 to given number using the For loop. for k in range(1, gvnNumbr+1): # Inside the For loop, Calculate the value of iterator value ^ iterator value # using the ** operator # and store it in a variable. powervl = k**k # Add the above variable to the resltsum resltsum = resltsum+powervl # Print the resltsum value which is the result of the series till the given Number N. print( 'The sum of the series till the given number {', gvnNumbr, '} is :', resltsum)
Output:
The sum of the series till the given number { 5 } is : 3413
Method #4: Using For loop and ** operator (User Input)
Approach:
- Give the number N as user input using the int(input()) function and store it in a variable.
- Take a variable say resltsum which gives the sum of the given series till N and initialize its value to 0.
- Loop from 1 to given number using the For loop.
- Inside the For loop, Calculate the value of iterator value ^ iterator value using the ** operator and store it in a variable.
- Add the above variable to the resltsum .
- Print the resltsum value which is the result of the series till the given Number N.
- The Exit of the Program.
Below is the implementation:
# Give the number N as user input using the int(input()) function # and store it in a variable. gvnNumbr = int(input('Enter some random number N = ')) # Take a variable say resltsum which gives the sum of # the given series till N and initialize its value to 0. resltsum = 0 # Loop from 1 to given number using the For loop. for k in range(1, gvnNumbr+1): # Inside the For loop, Calculate the value of iterator value ^ iterator value # using the ** operator # and store it in a variable. powervl = k**k # Add the above variable to the resltsum resltsum = resltsum+powervl # Print the resltsum value which is the result of the series till the given Number N. print( 'The sum of the series till the given number {', gvnNumbr, '} is :', resltsum)
Output:
Enter some random number N = 6 The sum of the series till the given number { 6 } is : 50069
Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.