In: Computer Science
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.
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