86.9k views
19 votes
Write python program to find sum(using while/loop): sum= 1+ 2+ 4+8+ 16+ 32+....1024

User Bhell
by
4.8k points

1 Answer

7 votes

Answer:

i = 0

total = 0

while i <= 10:

total += pow(2, i)

i += 1

print(total)

Step-by-step explanation:

When you look the equation, you would realize that it is the sum of the numbers that are 2 to the power of i where 0 <= i <= 10

Set i and total as 0

Create a while loop that iterates while i is smaller than or equal to 10. Add the 2 to the power of i to the total (cumulative sum). Note that we use pow() method to calculate the power. At the end of the loop, increment the i by 1. Otherwise the loop will be an infinite loop.

When the loop is done, print the total

User Sdolgy
by
5.0k points