C# 程式可將列表中的所有數字相乘
首先,設定列表 −
List<int> myList = new List<int> () {
5,
10,
7
};現在,將變數的值設定為 1,它將幫助我們進行乘法 −
int prod = 1;
迴圈並獲取產品 −
foreach(int i in myList) {
prod = prod*i;
}以下為程式碼 −
示例
using System;
using System.Collections.Generic;
public class Program {
public static void Main() {
List<int> myList = new List<int>() {
5,
10,
7
};
Console.WriteLine("List: ");
foreach(int i in myList) {
Console.WriteLine(i);
}
int prod = 1;
foreach(int i in myList) {
prod = prod*i;
}
Console.WriteLine("Product: {0}",prod);
}
}輸出
List: 5 10 7 Product: 350
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP