Giả sử bạn cần tìm các phần tử trong danh sách sau lớn hơn 80.
int[] arr = new int[] {55, 100, 87, 45}; Đối với điều đó, hãy lặp lại cho đến khi độ dài mảng. Ở đây, res =80, tức là phần tử đã cho.
for (int i = 0; i < arr.Length; i++) {
if(arr[i]<res) {
Console.WriteLine(arr[i]);
}
} Sau đây là mã hoàn chỉnh -
Ví dụ
using System;
namespace Demo {
public class Program {
public static void Main(string[] args) {
int[] arr = new int[] {
55,
100,
87,
45
};
// given integer
int res = 80;
Console.WriteLine("Given Integer {0}: ", res);
Console.WriteLine("Numbers larger than {0} = ", res);
for (int i = 0; i < arr.Length; i++) {
if (arr[i] > res) {
Console.WriteLine(arr[i]);
}
}
}
}
}