📌  相关文章
📜  国际空间研究组织 | ISRO CS 2013 |问题 32(1)

📅  最后修改于: 2023-12-03 14:50:46.751000             🧑  作者: Mango

ISRO CS 2013 - Question 32

This is a programming question from ISRO CS 2013. The problem statement is as follows:

Write a program to find the sum of series 1^1+2^2+3^3+...+n^n
Algorithm

To solve this problem, we need to calculate the sum of the series 1^1+2^2+3^3+...+n^n. We'll use a loop to iterate over the values of i from 1 to n. In each iteration, we'll calculate i^i and add it to a variable sum.

Finally, we'll print the value of sum.

The algorithm can be summarised with these steps:

  1. Set sum to 0.
  2. For each value of i from 1 to n do steps 3-4:
  3. Calculate i^i and add it to sum.
  4. End loop.
  5. Print the value of sum.
Solution

Here is the Python 3 code to solve this problem:

n = int(input("Enter a number: "))

sum = 0
for i in range(1, n+1):
    sum += i**i

print("The sum of the series is:", sum)

Here is the explanation of the code:

  • We first input the value of n from the user.
  • We initialise the variable sum to 0.
  • We use a for loop to iterate over the values of i from 1 to n.
  • In each iteration of the loop, we calculate i^i using the ** operator (exponentiation) and add it to the variable sum.
  • After the loop completes, we print the value of sum.
Conclusion

We have successfully implemented the algorithm to find the sum of the series 1^1+2^2+3^3+...+n^n. This code can be easily modified to find the sum of other similar series.