Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

목록의 모든 숫자를 곱하는 Python 프로그램?

<시간/>

먼저 사용자 입력을 위한 3개의 목록을 만듭니다. 여기에서는 탐색 기술을 사용합니다. product의 값을 1로 초기화하고 모든 요소를 ​​순회하고 목록의 끝까지 모든 숫자에 product를 1씩 곱합니다.

Input: A=[5,6,3]
Output:90
Explanation:5*6*3

알고리즘

Step 1:  input all numbers in the list (lst).
Step 2:  to multiply all values in the list we use traversing technique.	
Step 3:  variable X=1.
Step 4:  for i in LST		/*traverse from first to last in the list
          X=X*i		/* multiply elements one by one
Step 5:  display X

예시 코드

#To multiply all numbers in a list
def mulallnum(lst):
   x=1
   for i in lst:
      x=x*i
      return x
   #driver code
A=list()
B=list()
C=list()
n1=int(input("Enter the size of the First List ::"))
n2=int(input("Enter the size of the Second List ::"))
n3=int(input("Enter the size of the Third List ::"))
print("Enter the Element of First List ::")
for i in range(int(n1)):
   k=int(input(""))
   A.append(k)
print("Enter the Element of Second List ::")
for j in range(int(n2)):
   k1=int(input(""))
   B.append(k1)
print("Enter the Element of Third List ::")
for j in range(int(n3)):
   k1=int(input(""))
   C.append(k1)
print("MULTIPLY OF ALL NUMBERS IN FIRST LIST ::>",mulallnum(A))
print("MULTIPLY OF ALL NUMBERS IN SECOND LIST ::>",mulallnum(B))
print("MULTIPLY OF ALL NUMBERS IN THIRD LIST ::>",mulallnum(C))

출력

Enter the size of the First List ::3
Enter the size of the Second List ::4
Enter the size of the Third List ::5
Enter the Element of First List ::
1
2
5
Enter the Element of Second List ::
3
2
4
5
Enter the Element of Third List ::
12
2
1
3
2
MULTIPLY OF ALL NUMBERS IN FIRST LIST ::> 10
MULTIPLY OF ALL NUMBERS IN SECOND LIST ::> 120
MULTIPLY OF ALL NUMBERS IN THIRD LIST ::> 144