Tải bản đầy đủ (.docx) (9 trang)

Một số bài tập C# cơ bản và nâng cao

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (71.36 KB, 9 trang )

Bài tập C# - Lê Quang Trọng Tài

BÀI TẬP KIỂM TRA 2 SỐ BẰNG NHAU:
using System;
namespace BAITAP
{
class CsharpTL
{
public static void Main()
{

int int1, int2;
Console.Write("\n\n");
Console.Write("Chuong trinh C# kiem tra hai so co bang nhau khong:\n");
Console.Write("-------------------------------------------");
Console.Write("\n\n");
Console.Write("Nhap so thu nhat: ");
int1 = Convert.ToInt32(Console.ReadLine());

Console.Write("Nhap so thu hai: ");
int2 = Convert.ToInt32(Console.ReadLine());

if (int1 == int2)
Console.WriteLine("{0} va {1} la bang nhau.\n", int1, int2);
else
Console.WriteLine("{0} va {1} la khong bang nhau.\n", int1, int2);

Console.ReadKey();
}
}
}


BÀI TẬP TÍNH TỔNG DÃY SỐ
using System;
namespace BAITAP


Bài tập C# - Lê Quang Trọng Tài

{
class CsharpTL
{
public static void Main()
{

int j, sum = 0;

Console.Write("\n");
Console.Write("Tinh tong day so trong C#:\n");
Console.Write("-----------------------------");
Console.Write("\n\n");

Console.Write("10 so tu nhien dau tien la:\n");
for (j = 1; j %lt;= 10; j++)
{
sum = sum + j;
Console.Write("{0} ", j);
}
Console.Write("\nTong cua 10 so tren la: {0}\n", sum);

Console.ReadKey();
}

}
}
using System;
KIỂM TRA SỐ NGUYÊN TỐ
namespace BAITAP
{
class CsharpTL
{
public static void Main()
{


Bài tập C# - Lê Quang Trọng Tài

Console.Write("\n");
Console.Write("Kiem tra so nguyen to trong C#:\n");
Console.Write("----------------------------");
Console.Write("\n\n");

int number;
int bien_dem = 0;

Console.Write("Nhap mot so bat ky: ");
number = Convert.ToInt32(Console.ReadLine());

for (int i = 1; i <= number; i++)
if (number % i == 0)
bien_dem++;

if (bien_dem == 2)

Console.WriteLine("So da cho la so nguyen to.");
else
Console.WriteLine("So da cho khong phai la so nguyen to.");

Console.ReadKey();
}
}
}
TÍNH TỔNG CÁC SỐ LẺ
using System;
namespace BAITAP
{
class CsharpTL
{
public static void Main()
{


Bài tập C# - Lê Quang Trọng Tài

int i, n, sum = 0;
Console.Write("\n");
Console.Write("Hien thi va tinh tong n so le trong C#:\n");
Console.Write("------------------------------------------");
Console.Write("\n\n");

Console.Write("Nhap so cac so: ");
n = Convert.ToInt32(Console.ReadLine());
Console.Write("\nHien thi cac so le: ");
for (i = 1; i <= n; i++)

{
Console.Write("{0} ", 2 * i - 1);
sum += 2 * i - 1;
}
Console.Write("\nTong {0} so le ban dau la: {1} \n", n, sum);

Console.ReadKey();
}
}
}
KIỂM TRA SỐ CHẲN LẺ:
using System;

namespace BAITAP
{
class CsharpTL
{
public static void Main()
{

int num1, rem1;
Console.Write("\n\n");


Bài tập C# - Lê Quang Trọng Tài

Console.Write("Kiem tra chan le trong C#:\n");
Console.Write("---------------------------------------");
Console.Write("\n\n");
Console.Write("Nhap mot so nguyen bat ky: ");

num1 = Convert.ToInt32(Console.ReadLine());
rem1 = num1 % 2;
if (rem1 == 0)
Console.WriteLine("{0} la so chan.\n", num1);
else
Console.WriteLine("{0} la so le.\n", num1);

Console.ReadKey();
}
}
}
TÌM SỐ LỚN NHẤT:
using System;

namespace BAITAP
{
class CsharpTL
{
public static void Main()
{

int num1, num2, num3;
Console.Write("\n");
Console.Write("Chuong trinh C# de tim so lon nhat trong 3 so:\n");
Console.Write("------------------------------------");
Console.Write("\n\n");

Console.Write("Nhap so thu nhat: ");
num1 = Convert.ToInt32(Console.ReadLine());



Bài tập C# - Lê Quang Trọng Tài

Console.Write("Nhap so thu hai: ");
num2 = Convert.ToInt32(Console.ReadLine());
Console.Write("Nhap so thu ba: ");
num3 = Convert.ToInt32(Console.ReadLine());

if (num1 > num2)
{
if (num1 > num3)
{
Console.Write("So thu nhat la so lon nhat trong ba so. \n\n");
}
else
{
Console.Write("So thu ba la so lon nhat trong ba so. \n\n");
}
}
else if (num2 > num3)
Console.Write("So thu hai la so lon nhat trong ba so.\n\n");
else
Console.Write("So thu ba la so lon nhat trong ba so. \n\n");

Console.ReadKey();
}
}
}
TÍNH DIỆN TÍCH HÌNH:
using System;

namespace BAITAP
{
class CsharpTL
{
public static void Main()
{


Bài tập C# - Lê Quang Trọng Tài

int lua_chon, r, l, w, b, h;
double dien_tich = 0;

Console.Write("\n");
Console.Write("Chuong trinh C# duoi dang mot menu co cac lua chon de tinh dien
tich:\n");
Console.Write("--------------------------------------------------------------------");
Console.Write("\n\n");

Console.Write("Lua chon 1: tinh dien tich hinh tron\n");
Console.Write("Lua chon 2: tinh dien tich hinh chu nhat\n");
Console.Write("Lua chon 3: tinh dien tich hinh tam giac\n");
Console.Write("Nhap lua chon cua ban: ");
lua_chon = Convert.ToInt32(Console.ReadLine());

switch (lua_chon)
{
case 1:
Console.Write("Nhap ban kinh hinh trong: ");
r = Convert.ToInt32(Console.ReadLine());

dien_tich = 3.14 * r * r;
break;
case 2:
Console.Write("Nhap chieu dai HCN: ");
l = Convert.ToInt32(Console.ReadLine());
Console.Write("Nhap chieu rong HCN: ");
w = Convert.ToInt32(Console.ReadLine());
dien_tich = l * w;
break;
case 3:


Bài tập C# - Lê Quang Trọng Tài

Console.Write("Nhap canh huyen tam giac:");
b = Convert.ToInt32(Console.ReadLine());
Console.Write("Nhap chieu cao tam giac:");
h = Convert.ToInt32(Console.ReadLine());
dien_tich = 0.5 * b * h;
break;
}
Console.Write("Dien tich hinh la: {0}\n", dien_tich);

Console.ReadKey();
}
}
}
TÍNH GIAI THỪA:
using System;


namespace BAITAP
{
class CsharpTL
{
public static void Main()
{

int i, f = 1, num;

Console.Write("\n");
Console.Write("Tim giai thua trong C#:\n");
Console.Write("------------------------");
Console.Write("\n\n");

Console.Write("Nhap mot so bat ky: ");
num = Convert.ToInt32(Console.ReadLine());
for (i = 1; i <= num; i++)


Bài tập C# - Lê Quang Trọng Tài

f = f * i;

Console.Write("Giai thua cua {0} la: {1}\n", num, f);

Console.ReadKey();
}
}
}




×