04-01 while loops

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Assume there are two variables, k and m, each already assigned a positive integer value and further assume that k's value is smaller than m's. Write the code necessary to compute the number of perfect squares between k and m. (A perfect square is an integer like 9, 16, 25, 36 that is equal to the square of another integer (in this case 3*3, 4*4, 5*5, 6*6 respectively). Assign the number you compute to the variable q. For example, if k and m had the values 10 and 40 respectively, you would assign 3 to q because between 10 and 40 there are these perfect squares: 16, 25, and 36.

i = 1 q = 0 while i * i < k : i += 1 while i * i <= m : [Tab Key]q += 1 [Tab Key]i += 1

Assume there is a variable, h that has already been assigned a positive integer value. Write the code necessary to compute the sum of the perfect squares whose value is less than h, starting with 1. (A perfect square is an integer like 9, 16, 25, 36 that is equal to the square of another integer (in this case 3*3, 4*4, 5*5, 6*6 respectively). Assign the sum you compute to the variable q. For example, if h is 19, you would assign 30 to q because the perfect squares (starting with 1) that are less than h are: 1, 4, 9, 16 and 30==1+4+9+16.

q = 0 i = 1 while (i * i < h) : [Tab Key]q += i * i [Tab Key]i += 1

Assume there is a variable, h that has already been assigned a positive integer value. Write the code necessary to count the number of perfect squares whose value is less than h, starting with 1. (A perfect square is an integer like 9, 16, 25, 36 that is equal to the square of another integer (in this case 3*3, 4*4, 5*5, 6*6 respectively). Assign the sum you compute to a variable q For example, if h is 19, you would assign 4 to q because there are perfect squares (starting with 1) that are less than h are: 1, 4, 9, 16.

q = 0 i = 1 while i * i < h : [Tab Key]q += 1 [Tab Key]i += 1

Given that n refers to a positive integer use a while loop to compute the sum of the cubes of the first n counting numbers, and assign this value to total. Use no variables other than n, k, and total.

total = 0 k = 0 while k <= n: [Tab Key]total += k * k * k [Tab Key]k += 1

Use the variables k and total to write a while loop that computes the sum of the squares of the first 50 counting numbers, and assigns that value to total. Thus your code should assign 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 to total. Use no variables other than k and total.

total = 0 k = 50 while k>0: [Tab Key]total += k * k [Tab Key]k -= 1


Kaugnay na mga set ng pag-aaral

Elevate Q-Cards Module 2 Cardiac & Renal

View Set

BUS LAW CHP 37: Partnerships and LLP's

View Set

US History Chapter 11 Conspiracy and President Stories

View Set

Wheels in Motion Defensive Driving Exam

View Set

English IV Catcher Test Chapters 1-13

View Set

[Middle Ages, Renaissance, Baroque] in Enjoyment of Music

View Set