Write python program for the following questions:-
1. Write a program using for loop in python to print first 10 multiple of 2.
2. Write a program using loop in python to display factors of number N.
Answers
Answer:
The given programs are solved in Python language.
1. To print first 10 multiples of 2.
First approach:
print("First 10 multiples of 2 are...")
for i in range(1,11):
print(2*i,end=" ")
Second approach:
print("First 10 multiples of 2 are..")
print(" ".jóin(str(2*i) for i in range(1,11)))
2. To display the factors of n.
First approach:
n,a=int(input("Enter a number: ")),[]
print(f"Factors of {n} are:",end=" ")
for i in range(1,n+1):
if n%i==0:
a.append(i)
print(*a,sep=", ")
Second approach:
n=int(input("Enter a number: "))
print(f"Factors of {n} are:",end=" ")
print(*[i for i in range(1,n+1) if n%i==0],sep=", ")
Refer to the attachment for output.
•••♪
Question 1
#python program to print first ten multiple of 2
for I in range(1,11):
print(I*2)
Question 2:-
# Python program to display factors of a number
n=int(input("Enter a number:"))
for I in range(1,n+1):
if n%I==0:
print (I)