Share 1000 bài tập c#

minhquoctran

New member
#1000Exercisesc # #C ##Programming #tutorial #LearNtoCode ## 1000 Bài tập trong C #

1. ** Viết một chương trình để in Hello World. **

`` `C#
// Chương trình này in "Hello World!"đến giao diện điều khiển.

Console.WriteLine ("Hello World!");
`` `

2. ** Viết một chương trình để thêm hai số. **

`` `C#
// Chương trình này thêm hai số và in kết quả vào bảng điều khiển.

int num1 = 10;
int num2 = 20;
int sum = num1 + num2;

Console.WriteLine ("tổng của {0} và {1} là {2}", num1, num2, sum);
`` `

3. ** Viết một chương trình để in chuỗi Fibonacci. **

Trình tự Fibonacci là một loạt các số trong đó mỗi số là tổng của hai số trước.Trình tự bắt đầu bằng 0 và 1, vì vậy 10 số đầu tiên trong chuỗi là:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34

Bạn có thể viết một chương trình để in chuỗi Fibonacci bằng thuật toán sau:

`` `C#
// Chương trình này in 10 số đầu tiên trong chuỗi Fibonacci.

int n1 = 0;
int n2 = 1;
int n;

for (int i = 0; i <10; i ++) {
n = n1 + n2;
Console.WriteLine (n);

n1 = n2;
n2 = n;
}
`` `

4. ** Viết một chương trình để kiểm tra xem một số là số nguyên tố. **

Một số nguyên tố là một số tự nhiên lớn hơn 1 không phải là sản phẩm của hai số tự nhiên nhỏ hơn.Ví dụ: 2, 3, 5 và 7 là số nguyên tố, trong khi 4, 6 và 8 không phải là số nguyên tố vì chúng chia hết cho 2.

Bạn có thể viết một chương trình để kiểm tra xem một số chính là sử dụng thuật toán sau:

`` `C#
// Chương trình này kiểm tra xem một số là chính.

int số = 2;
bool isprime = true;

for (int i = 2; i <= math.sqrt (number); i ++) {
if (số % i == 0) {
isprime = false;
phá vỡ;
}
}

if (isprime) {
Console.WriteLine ("Số {0} là số nguyên tố.", Số);
} khác {
Console.WriteLine ("Số {0} không phải là nguyên tố.", Số);
}
`` `

5. ** Viết một chương trình để chơi một trò chơi của tic-tac-toe. **

Tic-Tac-Toe là một trò chơi hai người chơi được chơi trên lưới 3x3.Các cầu thủ thay phiên nhau đặt điểm (X hoặc O) của họ trong các hình vuông trống.Người chơi đầu tiên nhận được ba điểm của họ liên tiếp, cột hoặc đường chéo thắng trò chơi.

Bạn có thể viết một chương trình để chơi một trò chơi tic-tac-toe bằng thuật toán sau:

`` `C#
// Chương trình này chơi một trò chơi tic-tac-toe.

// Tạo lưới 3x3.
int [,] board = new int [3, 3];

// Khởi tạo lưới vào tất cả các hình vuông trống.
for (int i = 0; i <3; i ++) {
for (int j = 0; j <3; j ++) {
Bảng [i, j] = 0;
}
}

// có được di chuyển của người chơi.
int Player = 1;
hàng int, cột;
LÀM {
// có được di chuyển của người chơi.
Console.WriteLine ("Nhập di chuyển của bạn (hàng, cột):");
hàng = int.Parse (Console.Readline ());
cột
=======================================
#1000Exercisesc# #C# #Programming #tutorial #LearNtoCode ##1000 Exercises in C#

1. **Write a program to print Hello World.**

```c#
// This program prints "Hello World!" to the console.

Console.WriteLine("Hello World!");
```

2. **Write a program to add two numbers.**

```c#
// This program adds two numbers and prints the result to the console.

int num1 = 10;
int num2 = 20;
int sum = num1 + num2;

Console.WriteLine("The sum of {0} and {1} is {2}", num1, num2, sum);
```

3. **Write a program to print the fibonacci sequence.**

The fibonacci sequence is a series of numbers where each number is the sum of the two previous numbers. The sequence starts with 0 and 1, so the first 10 numbers in the sequence are:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34

You can write a program to print the fibonacci sequence using the following algorithm:

```c#
// This program prints the first 10 numbers in the fibonacci sequence.

int n1 = 0;
int n2 = 1;
int n;

for (int i = 0; i < 10; i++) {
n = n1 + n2;
Console.WriteLine(n);

n1 = n2;
n2 = n;
}
```

4. **Write a program to check if a number is prime.**

A prime number is a natural number greater than 1 that is not a product of two smaller natural numbers. For example, 2, 3, 5, and 7 are prime numbers, while 4, 6, and 8 are not prime numbers because they are divisible by 2.

You can write a program to check if a number is prime using the following algorithm:

```c#
// This program checks if a number is prime.

int number = 2;
bool isPrime = true;

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

if (isPrime) {
Console.WriteLine("The number {0} is prime.", number);
} else {
Console.WriteLine("The number {0} is not prime.", number);
}
```

5. **Write a program to play a game of tic-tac-toe.**

Tic-tac-toe is a two-player game played on a 3x3 grid. The players take turns placing their marks (X or O) in empty squares. The first player to get three of their marks in a row, column, or diagonal wins the game.

You can write a program to play a game of tic-tac-toe using the following algorithm:

```c#
// This program plays a game of tic-tac-toe.

// Create a 3x3 grid.
int[,] board = new int[3, 3];

// Initialize the grid to all empty squares.
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board[i, j] = 0;
}
}

// Get the player's move.
int player = 1;
int row, column;
do {
// Get the player's move.
Console.WriteLine("Enter your move (row, column): ");
row = int.Parse(Console.ReadLine());
column
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top