Mảng và vòng lặp:
[codes=c#]using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Buoi2_ArrayLoop
{
class Program
{
static void Main(string[] args)
{
const int max =5;
int[] m = new int[max];
//Nhap du lieu cho mang
for(int i=0; i
Console.Write(“a[{0}]=”,i);
m[i]=Convert.ToInt32(Console.ReadLine());
}
// Không cần truy cập qua chỉ sô
Console.WriteLine(“Mang vua nhap:”);
foreach(int i in m)
{
Console.Write(i+” “);
}
Console.WriteLine();
//Dung vong lap While de in ra man hinh cac vi tri le cua mang
Console.WriteLine(“Vi tri le”);
int j = 1;
while (j
Console.Write(m[j] + ” “);
j += 2;
}
// dung vong lap do while in vi tri chan
Console.WriteLine();
Console.WriteLine(“Vi tri chan”);
int k = 0;
do
{
Console.Write(m[k] + ” “);
k += 2;
} while(k
}
}
[/codes]
Cấu trúc Swich:
[codes=c#]using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Buoi2_Switch
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(“Nhap vao thu trong tuan”);
int i = Convert.ToInt32(Console.ReadLine());
switch (i)
{
case 2: Console.WriteLine(“Thu 2”); break;
case 3: Console.WriteLine(“Thu 3”); break;
case 4: Console.WriteLine(“Thu 4”); break;
case 5: Console.WriteLine(“Thu 5”); break;
case 6: Console.WriteLine(“Thu 6”); break;
case 7: Console.WriteLine(“Thu 7”); break;
case 8: Console.WriteLine(“CN”); break;
default: Console.WriteLine(“Khong la thu trong tuan”); break;
}
Console.ReadLine();
}
}
}[/codes]
[codes=c#]using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace vdu2
{
class Program
{
static void Main(string[] args)
{
//Mang mot chieu
Console.WriteLine(“Nhap so Pt cua mang”);
int n = Convert.ToInt32(Console.ReadLine());
int[] a =new int[n];
for (int i = 0; i < n; i++)
{
Console.Write(“a[{0}]=”, i);
a[i] = Convert.ToInt32(Console.ReadLine());
}
foreach (int j in a)
{
if (ktsnt(j) == 0)
Console.WriteLine(“{0}”, j);
}
/*
* Kiem tra so nguyen to
* string c;
do
{
Console.WriteLine(“Nhap vao so can kt”);
int n = Convert.ToInt32(Console.ReadLine());
int i = 2, kt = 0;
while (i < n)
{
if (n % i == 0)
{
kt = 1;
break;
}
i++;
}
if (kt == 0)
Console.WriteLine(“La SNT”);
else
Console.WriteLine(“k0 la SNT”);
Console.WriteLine(“Co can kien tra nua khong y/n”);
c = Console.ReadLine();
}
while (c == “y”);
//Thu trong tuan
* switch (n)
{
case 2: Console.WriteLine(“Thu 2”);
break;
case 3: Console.WriteLine(“Thu 3”);
break;
case 4: Console.WriteLine(“Thu 4”);
break;
case 5: Console.WriteLine(“Thu 5”);
break;
case 6: Console.WriteLine(“Thu 6”);
break;
case 7: Console.WriteLine(“Thu 7”);
break;
case 8: Console.WriteLine(“CN”);
break;
default: Console.WriteLine(“Khong phai thu trong tuan”);
break;
}*/
Console.ReadLine();
}
static int ktsnt(int n)
{
int i = 2, kt = 0;
while (i < n)
{
if (n % i == 0)
{
kt = 1;
break;
}
i++;
}
return kt;
}
}
}[/codes]
Last modified on December 4th, 2020 at 3:17 am
Nam Le
lequocnam
0 responds