0% found this document useful (0 votes)
0 views12 pages

CCCCC

The document contains multiple C# programs that demonstrate various functionalities such as solving quadratic equations, determining if a number is even or odd, grading based on scores, calculating dates, checking for prime numbers, and computing the harmonic sum of integers. Each program includes user input prompts and outputs results based on the calculations performed. The code is structured with functions to handle specific tasks, enhancing modularity and readability.

Uploaded by

nhat48350
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views12 pages

CCCCC

The document contains multiple C# programs that demonstrate various functionalities such as solving quadratic equations, determining if a number is even or odd, grading based on scores, calculating dates, checking for prime numbers, and computing the harmonic sum of integers. Each program includes user input prompts and outputs results based on the calculations performed. The code is structured with functions to handle specific tasks, enhancing modularity and readability.

Uploaded by

nhat48350
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Bai3

static void Main(string[] args)


{
Console.WriteLine("Chuong Trinh C# Giai Phuong Trinh Bac 2");
Console.WriteLine("---------------------------------------------");
Console.WriteLine(" ");
Console.Write("Nhap a: ");
double a = double.Parse(Console.ReadLine());
Console.Write("Nhap b: ");
double b = double.Parse(Console.ReadLine());
Console.Write("Nhap c: ");
double c = double.Parse(Console.ReadLine());
Console.WriteLine("---------------------------------------------");
double result;
double detal = (b * b - 4 * a * c);
if (a == 0 && b != 0 && c != 0)
{
result = (-c / b);
Console.WriteLine($"Do a = 0 Nen Phuong Trinh Thanh bx+c = 0");
Console.WriteLine($"Ket Qua:{result}");
}

else if (a == 0 && b == 0 && c == 0)


{
Console.WriteLine("Phuong Trinh Co Vo So Nghiem.");
}
else if (a == 0 && b == 0 && c != 0)
{
Console.WriteLine("Phuong Trinh Vo Nghiem.");
}

if (a != 0)
{

if (detal > 0)
{
Console.WriteLine("Phuong Trinh Co 2 Nghiem Phan Biet:");
double x1 = (-b + Math.Sqrt(detal)) / (2 * a);
double x2 = (-b - Math.Sqrt(detal)) / (2 * a);
Console.WriteLine($"Nghiem x1 = {x1}");
Console.WriteLine($"Nghiem x2 = {x2}");
}
if (detal == 0)
{
Console.WriteLine("Phuong Trinh Co 1 Nghiem Kep");
double x = -b / (2 * a);
Console.WriteLine($"Ket Qua:{x}");
}
if (detal < 0)
{
Console.WriteLine("Phuong Trinh Vo Nghiem (khong co nghiem thuc, chi co nghiem
phuc)");
}

}
Console.ReadKey();
}
}

Bai 7
static void Main(string[] args)
{
Console.Write("Nhap mot so: ");
int number = int.Parse(Console.ReadLine());
if (number % 2 == 0)
{
Console.WriteLine("So chan");
}
else
{
Console.WriteLine("So le");
}
Console.ReadKey();

Bai 3
static void Main(string[] args)
{
Console.Write("Nhap diem tong ket: ");
double diemTK = double.Parse(Console.ReadLine());
switch (diemTK)
{
case double d when (d >= 0 && d <= 3.0):
Console.WriteLine("Hoc luc: Kem");
break;
case double d when (d > 3.0 && d <= 4.0):
Console.WriteLine("Hoc luc: Yeu");
break;
case double d when (d > 4.0 && d <= 6.0):
Console.WriteLine("Hoc luc: Trung binh");
break;
case double d when (d > 6.0 && d <= 8.0):
Console.WriteLine("Hoc luc: Kha");
break;
case double d when (d > 8.0 && d <= 10.0):
Console.WriteLine("Hoc luc: Gioi");
break;
default:
Console.WriteLine("Diem khong hop le.");
break;
}
Console.ReadKey();

Bai7
static void Main(string[] args)
{
Console.Write("Nhap ngay: ");
int day = int.Parse(Console.ReadLine());
Console.Write("Nhap thang: ");
int month = int.Parse(Console.ReadLine());
Console.Write("Nhap nam: ");
int year = int.Parse(Console.ReadLine());

try
{
DateTime currentDate = new DateTime(year, month, day);
DateTime yesterday = currentDate.AddDays(-1);
DateTime tomorrow = currentDate.AddDays(1);

Console.WriteLine($"Ngay hom qua: {yesterday:dd/MM/yyyy}");


Console.WriteLine($"Ngay mai: {tomorrow:dd/MM/yyyy}");
}
catch (ArgumentOutOfRangeException)
{
Console.WriteLine("Ngay thang nam khong hop le.");
}

Console.ReadKey();
}
Bai 3
static void Main(string[] args)
{
Console.Write("Nhap mot so: ");
int n = int.Parse(Console.ReadLine());

if (n < 2)
{
Console.WriteLine("Khong phai so nguyen to.");
return;
}

bool laSoNguyenTo = true;


for (int i = 2; i <= Math.Sqrt(n); i++)
{
if (n % i == 0)
{
laSoNguyenTo = false;
break;
}
}

if (laSoNguyenTo)
Console.WriteLine("La so nguyen to.");
else
Console.WriteLine("Khong phai so nguyen to.");
Console.ReadKey();

Bai 7
Console.Write("Nhap so nguyen n: ");
int n = int.Parse(Console.ReadLine());
double tong = 0;
for (int i = 1; i <= n; i++)
{
tong += 1.0 / i;
}
Console.WriteLine($"Tong nghich dao cua {n} so nguyen dau tien la: {tong:F4}");

3
Console.Write("Nhap mot so: ");
string number = Console.ReadLine();
string[] digits = { "Khong", "Mot", "Hai", "Ba", "Bon", "Nam", "Sau", "Bay", "Tam", "Chin" };
int j = 0;
while (j < number.Length)
{
if (char.IsDigit(number[j]))
{
int num = int.Parse(number[j].ToString());
Console.Write(digits[num] + " ");
}
j++;
}
Console.WriteLine();
Console.Re adKey();

3
static void NhapHeSo(out double a, out double b)
{
Console.Write("Nhap he so a: ");
a = double.Parse(Console.ReadLine());
Console.Write("Nhap he so b: ");
b = double.Parse(Console.ReadLine());
}

static void GiaiBatPhuongTrinh(double a, double b)


{
if (a == 0)
{
if (b > 0)
Console.WriteLine("Phuong trinh luon dung (tat ca x)");
else
Console.WriteLine("Phuong trinh vo nghiem");
}
else
{
double x = -b / a;
if (a > 0)
Console.WriteLine($"Phuong trinh co nghiem x > {x:F2}");
else
Console.WriteLine($"Phuong trinh co nghiem x < {x:F2}");
}
}

static void Main()


{
bool tiepTuc = true;
while (tiepTuc)
{
double a, b;
NhapHeSo(out a, out b);
GiaiBatPhuongTrinh(a, b);
Console.Write("Ban co muon giai tiep khong? (y/n): ");
char choice = char.Parse(Console.ReadLine().ToLower());
if (choice != 'y')
{
tiepTuc = false;
}
}
}

7
static void NhapHeSo(out double a, out double b)
{
Console.Write("Nhap he so a: ");
a = double.Parse(Console.ReadLine());
Console.Write("Nhap he so b: ");
b = double.Parse(Console.ReadLine());
}

static void GiaiBatPhuongTrinh(double a, double b)


{
if (a == 0)
{
if (b > 0)
Console.WriteLine("Phuong trinh luon dung (tat ca x)");
else
Console.WriteLine("Phuong trinh vo nghiem");
}
else
{
double x = -b / a;
if (a > 0)
Console.WriteLine($"Phuong trinh co nghiem x > {x:F2}");
else
Console.WriteLine($"Phuong trinh co nghiem x < {x:F2}");
}
}

static double TinhLuyThua(double x, int y)


{
double result = 1;
for (int i = 1; i <= y; i++)
{
result *= x;
}
return result;
}

static void Main()


{
bool tiepTuc = true;
while (tiepTuc)
{
double a, b;
NhapHeSo(out a, out b);
GiaiBatPhuongTrinh(a, b);
Console.Write("Ban co muon giai tiep khong? (y/n): ");
char choice = char.Parse(Console.ReadLine().ToLower());
if (choice != 'y')
{
tiepTuc = false;
}
}
Console.Write("Nhap gia tri x: ");
double xx = double.Parse(Console.ReadLine());
Console.Write("Nhap gia tri y: ");
int yy = int.Parse(Console.ReadLine());
double result = TinhLuyThua(xx, yy);
Console.WriteLine($"Gia tri cua {xx}^({yy}) la: {result}");
}

3
static void NhapHeSo(out double a, out double b)
{
Console.Write("Nhap he so a: ");
a = double.Parse(Console.ReadLine());
Console.Write("Nhap he so b: ");
b = double.Parse(Console.ReadLine());
}

static void GiaiBatPhuongTrinh(double a, double b)


{
if (a == 0)
{
if (b > 0)
Console.WriteLine("Phuong trinh luon dung (tat ca x)");
else
Console.WriteLine("Phuong trinh vo nghiem");
}
else
{
double x = -b / a;
if (a > 0)
Console.WriteLine($"Phuong trinh co nghiem x > {x:F2}");
else
Console.WriteLine($"Phuong trinh co nghiem x < {x:F2}");
}
}

static double TinhLuyThua(double x, int y)


{
double result = 1;
for (int i = 1; i <= y; i++)
{
result *= x;
}
return result;
}

static void Main()


{
bool tiepTuc = true;
while (tiepTuc)
{
double a, b;
NhapHeSo(out a, out b);
GiaiBatPhuongTrinh(a, b);
Console.Write("Ban co muon giai tiep khong? (y/n): ");
char choice = char.Parse(Console.ReadLine().ToLower());
if (choice != 'y')
{
tiepTuc = false;
}
}
Console.Write("Nhap gia tri x: ");
double xx = double.Parse(Console.ReadLine());
Console.Write("Nhap gia tri y: ");
int yy = int.Parse(Console.ReadLine());
double result = TinhLuyThua(xx, yy);
Console.WriteLine($"Gia tri cua {xx}^({yy}) la: {result}");
Console.Write("Nhap so luong phan tu trong mang: ");
int N = int.Parse(Console.ReadLine());
double[] arr = new double[N];
Console.WriteLine("Nhap cac phan tu cua mang:");
for (int i = 0; i < N; i++)
{
Console.Write($"Phan tu {i + 1}: ");
arr[i] = double.Parse(Console.ReadLine());
}

Console.WriteLine("Mang ban vua nhap la:");


for (int i = 0; i < N; i++)
{
Console.Write(arr[i] + " ");
}
Console.ReadKey();

7
static void NhapHeSo(out double a, out double b)
{
Console.Write("Nhap he so a: ");
a = double.Parse(Console.ReadLine());
Console.Write("Nhap he so b: ");
b = double.Parse(Console.ReadLine());
}

static void GiaiBatPhuongTrinh(double a, double b)


{
if (a == 0)
{
if (b > 0)
Console.WriteLine("Phuong trinh luon dung (tat ca x)");
else
Console.WriteLine("Phuong trinh vo nghiem");
}
else
{
double x = -b / a;
if (a > 0)
Console.WriteLine($"Phuong trinh co nghiem x > {x:F2}");
else
Console.WriteLine($"Phuong trinh co nghiem x < {x:F2}");
}
}

static double TinhLuyThua(double x, int y)


{
double result = 1;
for (int i = 1; i <= y; i++)
{
result *= x;
}
return result;
}

static void Main()


{
bool tiepTuc = true;
while (tiepTuc)
{
double a, b;
NhapHeSo(out a, out b);
GiaiBatPhuongTrinh(a, b);
Console.Write("Ban co muon giai tiep khong? (y/n): ");
char choice = char.Parse(Console.ReadLine().ToLower());
if (choice != 'y')
{
tiepTuc = false;
}
}
Console.Write("Nhap gia tri x: ");
double xx = double.Parse(Console.ReadLine());
Console.Write("Nhap gia tri y: ");
int yy = int.Parse(Console.ReadLine());
double result = TinhLuyThua(xx, yy);
Console.WriteLine($"Gia tri cua {xx}^({yy}) la: {result}");

Console.Write("Nhap so luong phan tu trong mang: ");


int N = int.Parse(Console.ReadLine());
double[] arr = new double[N];
Console.WriteLine("Nhap cac phan tu cua mang:");
for (int i = 0; i < N; i++)
{
Console.Write($"Phan tu {i + 1}: ");
arr[i] = double.Parse(Console.ReadLine());
}

Console.WriteLine("Mang ban vua nhap la:");


for (int i = 0; i < N; i++)
{
Console.Write(arr[i] + " ");
}

double sum = 0;
for (int i = 0; i < N; i++)
{
sum += arr[i];
}
double avg = sum / N;
Console.WriteLine($"\\nTrung binh cong cua mang la: {avg}");
Console.WriteLine("Cac phan tu lon hon trung binh cong la:");
bool found = false;
for (int i = 0; i < N; i++)
{
if (arr[i] > avg)
{
Console.Write(arr[i] + " ");
found = true;
}
}
if (!found)
{
Console.WriteLine("Khong co phan tu nao lon hon trung binh cong.");
}
Console.ReadKey();
}

You might also like