124k views
5 votes
Write a program with loops that compute: (10 points total) 1. Use a loop to sum the even numbers from 2 to 100 (inclusive) 2. Compute the sum of all of the squares from 1 to 10 (exlusive). 3. Compute all powers of 2 from 2 ** 0 to 2 ** 20 (inclusive). 4. Compute the sum of all odd integers between a and b(inclusive) where a and b are inputs. 5. Compute the sum of the odd digits in an input. For example: If the input is "32a7b79" then sum

1 Answer

1 vote

Answer:

Following are the code of this question:

print("1:")

even_sum= 0;#declaring even_sum variable

for i in range(2, 101):#defining loop to count number from 2 to 100

if(i%2 == 0):#defining if block to count even number

even_sum=even_sum+i#add even numbers in even_sum variable

print (even_sum)#print even_sum

print("2:")

sum_suares= 0#declaring sum_suares variable

for i in range(1, 11):#defining loop to count square from 1 to 10

sum_suares= sum_suares+i*i # add square value

print (sum_suares)#print sum_suares value

print("3:")

for i in range(0, 21):# defining loop to count from 0 to 20

print ("Power of 2^",i," = ",pow(2,i))#print value

print("4:")

a = int(input('Enter first number:'))#defining a variable for user input

b = int(input('Enter second number:'))#defining b variable for user input

odd_sum = 0#defining odd_sum variable and assign value 0

for i in range(a, b+1):# defining loop that count from A to B+1

if(i%2 == 1):# defining if condition to check odd numbers and add value in odd_sum

odd_sum =odd_sum+i#add value in odd_sum variable

print (odd_sum)#print odd_sum value

print("5:")

number=input("Enter number : ")#defining number varaible for user input

odd_sum=0 #defining variable odd_sum that store value 0

for i in number: #defining loop to count odd number checking each digit

if int(i)%2!=0: #checking odd number

odd_sum=odd_sum+int(i)#add odd numbers

print(odd_sum)

Output:

please find the attachment.

Step-by-step explanation:

Description of the given code can be defined as follows:

  • In the first program, it prints the sum of the even number from 2 to 100.
  • In the second program, it first calculates the square from 1 to 10 and then adds all square values.
  • In the third program, it calculates the power of the 2 from 0 to 20.
  • In the fourth program, it first inputs two values "a, b", after input it calculates odd number then adds its values.
  • In the fifth program, it inputs value from the user ends then, splits the value then adds odds number values.
Write a program with loops that compute: (10 points total) 1. Use a loop to sum the-example-1
User Caritos
by
7.6k points