Đây là bài tập cơ bản để bạn làm việc với mảng (array) và cách sử dụng mảng trong ngôn ngữ lập trình C-Sharp. Đề bài yêu cầu tạo ra một danh sách các con số được nhập vào từ bàn phím, và đếm xem có bao nhiêu số 1 xuất hiện trong danh sách này.
Mã Nguồn C# - C# Source Code:
Mã Nguồn C# - C# Source Code:
---------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSharpExample
{
class Count_The_Number_In_Array
{
static void Main(string[] args)
{
int m, count = 0;
Console.WriteLine("Enter the Limit : ");
m = int.Parse(Console.ReadLine());
int[] a = new int[m];
Console.WriteLine("Enter the Numbers :");
for (int i = 0; i < m; i++)
{
a[i] = Convert.ToInt32(Console.ReadLine());
}
foreach (int o in a)
{
if (o == 1)
{
count++;
}
}
Console.WriteLine("Number of 1's in the Entered Number : ");
Console.WriteLine(count);
Console.ReadLine();
}
}
}
----------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSharpExample
{
class Count_The_Number_In_Array
{
static void Main(string[] args)
{
int m, count = 0;
Console.WriteLine("Enter the Limit : ");
m = int.Parse(Console.ReadLine());
int[] a = new int[m];
Console.WriteLine("Enter the Numbers :");
for (int i = 0; i < m; i++)
{
a[i] = Convert.ToInt32(Console.ReadLine());
}
foreach (int o in a)
{
if (o == 1)
{
count++;
}
}
Console.WriteLine("Number of 1's in the Entered Number : ");
Console.WriteLine(count);
Console.ReadLine();
}
}
}
----------------
No comments:
Post a Comment