Question

In: Computer Science

This is Python coding question, and topic is loop Can anyone help me figuring these out?...

This is Python coding question, and topic is loop

Can anyone help me figuring these out?

(Please do not use build in function)

Thanks.

1. Write a program that prints your name 100 times to the screen.

2. Write a program that takes a string s and an integer n as parameters and prints the string s a total of in n times(once per line)

3. Write a for loop that prints all the integers from 3141 to 5926, skipping every other one.

4. Write a for loop that prints every 5th integer from 5926 down to 3141

5. Write a function square that takes two parameters, a string s and an integer l and prints a square of size l using the string. For example, square(‘‘Q’’, 5) should produce the following output:

   QQQQQ
   QQQQQ
   QQQQQ
   QQQQQ
   QQQQQ

6. Write a program (using a for loop) to compute the following sum and print only the total: 3^1 +3^2 +3^3 +...+3^20.

7. Write a function that takes two integer parameters a and n, and returns the sum a^1 +a^2 +...+a^n.

8. A mortgage loan is charged interest every month based on the current balance of the loan. If the annual rate is r%, then the interest for the month is r/12% of the current balance. After the interest is applied each month, the borrower makes a payment which reduces the amount owed. Write a function mortgage(principal, rate, years, payment) that prints out the balance at the end of each month foryears number of years.

9. Write a function that takes an integer parameter n and returns the nth Fibonacci number. The Fibonacci numbers start 1,1,2,3,5,8,... and every subsequent one is the sum of the previous two.

Solutions

Expert Solution

Program 1

name=input("Enter yor name : ")
for i in range(100):
print(name)

Screenshot

Program 2

s=input("Enter a string : ")
n=int(input("Enter an integer : "))
for i in range(n):
print(s)

Screenshot

Program 3

for i in range(3141,5927):
print(i)
Screenshot

Program 4

for i in range(5926,3141,-5):
print(i)

Screenshot

Program 5

def square(s,l):
for i in range(l):
for j in range(l):
print(s,end="")
print()
def main():
s=input("Enter a string : ")
l=int(input("Enter an integer : "))
square(s,l)
main()

Screenshot

Output

Enter a string : Q
Enter an integer : 5
QQQQQ
QQQQQ
QQQQQ
QQQQQ
QQQQQ

Program 6

total=0
for i in range(20):
total=total+pow(3,i)   
print(total)

Screenshot

Program 7

def total(a,n):
sum=0
for i in range(n):
sum=sum+pow(a,i)   
return sum

def main():
a=int(input("Enter a :"))
n=int(input("Enter n :"))
print("Sum =",total(a,n))
main()

Screenshot

Program 9

def Fibonacci(n):
if n<0:
print("Incorrect input")
elif n==0:
return 0
elif n==1:
return 1
else:
return Fibonacci(n-1)+Fibonacci(n-2)
  


def main():
n=int(input("Enter n :"))
print(Fibonacci(n))
  
main()

Output

Enter n :8
21

Screenshot


Related Solutions

can anyone please help me woth graphs and business report for business computing?
can anyone please help me woth graphs and business report for business computing?
Using C programming make one for loop Description For this problem you will be figuring out...
Using C programming make one for loop Description For this problem you will be figuring out if it is more beneficial to pay off your loans first before investing or if you should only make the minimum payments and invest the rest. Some things to pay attention to Interest rates given are annual interests rates but we will be assuming that interest is compounded monthly so the real rates to use will be 1/12 of what we are given We...
​​​​​​This is an assignment that I'm having trouble figuring out in python: Modify Node class so...
​​​​​​This is an assignment that I'm having trouble figuring out in python: Modify Node class so it has a field called frequency and initialize it as one. Add a search function. If a number is in the list, return its frequency. Modify Insert function. Remove push, append, and insertAfter as one function called insert(self, data). If you insert a data that is already in the list, simply increase this node’s frequency. If the number is not in the list, add...
can anyone summarize for me the below passage and to add for me some information that...
can anyone summarize for me the below passage and to add for me some information that u get from the below passage Thank You Theory of Reasoned Action stresses the importance of attitudes and intentions in changing a behavior. According to this theory, the most important determinant of behavior is intention.23 Very few actions that produce a healthy outcome happen without ample knowledge and full intention to practice the healthy behavior. Two cognitive processes are at work to develop healthy...
2) create a python program that uses a for loop and range to print out the...
2) create a python program that uses a for loop and range to print out the values 10 8 6 4 2 3) Create a python program that yses a for loop to print out ["bob","al","bert"]
Writing python code: Can you do for me this. The question looks bellow Overview : As...
Writing python code: Can you do for me this. The question looks bellow Overview : As a programmer, you have be asked by a good friend to create a program that will allow them to keep track of their personal expenses. They want to be able to enter their expenses for the month, store them in a file, and then have the ability to retrieve them later. However, your friend only wants certain people to have the ability to view...
PYTHON CODING: In this problem, we will figure out if two balls are colliding. We will...
PYTHON CODING: In this problem, we will figure out if two balls are colliding. We will think in 2D to simplify things, though 3D isn’t different conceptually. For calculating collision, we only care about a ball’s position in space, as well as its size. We can store a ball’s position with the (x, y) coordinates of its center point, and we can calculate its size if we know its radius. Thus, we represent a ball in 2D space as a...
Can anyone tell me about the measurement of ICP-AES?
Can anyone tell me about the measurement of ICP-AES?
Can anyone give me a summary on "Dismantling the doctrine of Discovery" ?
Can anyone give me a summary on "Dismantling the doctrine of Discovery" ?
Could anyone explain me in simple words what is being said in this topic from Feynman...
Could anyone explain me in simple words what is being said in this topic from Feynman Lectures Vol.1 Chapter 4 topic 4-2 Gravitational Potential Energy (great book and author btw)...please refer to the book (pdf on net or a copy) to get what i'm asking. i don't understand after it starts talking about perpetual motion and lifting and lowering of weights.please explain in simple words. so i got confused at this point- "Consider weight-lifting machines—machines which have the property that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT