python chp 4

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

In mathematics, the notation n! represents the factorial of the nonnegative integer n. The factorial of n is the product of all the nonnegative integers from 1 to n. For example: 7! = 1 x 2 x 3 x 4 x 5 x 6 x 7 = 5,040 Write a program that lets the user enter a nonnegative integer and then uses a loop to calculate the factorial of that number. Print the factorial to standard output

ans=1 n=int(input("Enter a nonnegative integer:")) while (n>0): ans *= n n=n-1 print(ans)

Associate the average of the numbers from 1 to n (where n is a positive integer value) with the variable avg.

avg = (1 + n) / 2

Assume there are two variables , k and m, each already associated with 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).) Associate the number you compute with 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: q+=1 i+=1

Given a variable n refers to a positive int value, use two additional variables , k and total to write a for loop to compute the sum of the cubes of the first n counting numbers, and store this value in total. Thus your code should put 1*1*1 + 2*2*2 + 3*3*3 +... + n*n*n into total. Use no variables other than n, k, and total.

k=0 total=0 for k in range (n): k+=1 total+=k*k*k

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 associates that value with total. Thus your code should associate 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 with total. Use no variables other than k and total.

k=0 total=0 while k<50: k+=1 total+=k*k

Given a variable profits, write a statement that increases its value by a factor of 10.

profits= profits * 10

Write a statement that increments total by the value associated with amount. That is, add the value associated with amount to that associated with total and assign the result to total.

total=(total + amount)

Use two variables k and total to write a for loop to compute the sum of the squares of the first 50 counting numbers, and store this value in total. Thus, your code should put 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 into total. Use no variables other than k and total.

total = sum(k*k for k in range (1, 51)) print='total'

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

total=0 k=1 while k<=n: total+=k**3 k+=1

You want to know your grade in Computer Science, so write a program that continuously takes grades between 0 and 100 to standard input until you input "stop", at which point it should print your average to standard output.

total=0 q=0 avg=0 score=input() while score != "stop": q+= 1 total=total+ int(score) avg= total/q score=input() print (avg)

At one college, the tuition for a full-time student is $8,000 per semester. It has been announced that the tuition will increase by 3 percent each year for the next 5 years. Write a program with a loop that displays the projected semester tuition amount for the next 5 years. The program should print out the result in the form In 1 year, the tuition will be $8002.3. In 2 years, the tuition will be $8103.2. In 3 years, ... In 4 years, ... In 5 years, ... (If, for example, the tuition would cost 8002.3 dollars in one year, etc.)

year=1 tuition=8000 while(year<=5): if (year==1): tuition=1.03*tuition print("In ", year," year, the tuition will be $", +(tuition),".",sep="") year=year+1 else: tuition=1.03*tuition print("In ", year," years, the tuition will be $", +(tuition),".",sep="") year=year+1

Given a positive integer n, assign True to is_prime if n has no factors other than 1 and itself. (Remember, m is a factor of n if m divides n evenly.)

if n == 2: is_prime = True elif n % 2 == 0: is_prime = False else: is_prime = True for m in range (3, int(n**0.5)+1, 2): if n % m == 0: is_prime = False


संबंधित स्टडी सेट्स

Business Law 235: Chapter 9 Contract Law (Questions Whether it is Contract Law or Not)

View Set

(1.1) Introduction to Psychology

View Set