fork download
  1. # your code goes here# Function to calculate sum of cubes of all numbers in the range [N, M]
  2. def sum_of_cubes(N, M):
  3. total_sum = 0
  4. for i in range(N, M + 1):
  5. total_sum += i ** 3 # Cube of the number
  6. return total_sum
  7.  
  8. # Taking input for N and M
  9. N = int(raw_input("Enter the value of N: "))
  10. M = int(raw_input("Enter the value of M: "))
  11.  
  12. # Check that N <= M
  13. if N <= M:
  14. result = sum_of_cubes(N, M)
  15. print("The sum of cubes of numbers from {} to {} is: {}".format(N, M, result))
  16. else:
  17. print("Invalid input. Make sure N is less than or equal to M.")
  18.  
Success #stdin #stdout 0.01s 7256KB
stdin
2
4
stdout
Enter the value of N: Enter the value of M: The sum of cubes of numbers from 2 to 4 is: 99