4 Loop

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

4.Program using loops.

i) using while loop Multiplication Table


print("Multiplication table using while")
n=int(input("How many times :"))
x=int(input("Which table you want :"))
i=1
while(i<=n):
z=x*i
print(x,"*",i,"=",z)
i=i+1

Output
Multiplication table using while
How many times :10
Which table you want :2
2*1=2
2*2=4
2*3=6
2*4=8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20

ii) using for loop Factorial

n=int(input("Give the n value find Factorial:"))


f=1
for i in range(1,n+1):
f=f*i
print("factorial of",n,"is",f)

Output
Give the n value find Factorial:5
factorial of 5 is 120

iii) using Nested for loop Pattern

n=int(input("Give the n value :"))


for i in range(1,n+1):
for j in range(1,i+1):
print(j,end=" ")
print("\n")
Output
Give the n value :5
1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

You might also like