Share bài tập tin học 11 c++

ducphilykha

New member
#CNH11 #C ++ #exercise #Programming #Coding ## CNH 11 C ++ Bài tập

1. ** Viết một chương trình để in 100 số Fibonacci đầu tiên. **

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.Hai số đầu tiên trong chuỗi là 0 và 1. Vì vậy, 100 số Fibonacci đầu tiên là:

`` `
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368,75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817 7914296, 433494437, 701408733, 1134903170, 1836311903, 2971215073, 4807526976, 7778742049
`` `

Dưới đây là một chương trình trong C ++ in 100 số Fibonacci đầu tiên:

`` `C ++
#include <Istream>

sử dụng không gian tên STD;

int main () {
// Xác định hai số Fibonacci đầu tiên.
int a = 0;
int b = 1;

// In 100 số Fibonacci đầu tiên.
for (int i = 0; i <100; i ++) {
// Tính số Fibonacci tiếp theo.
int c = a + b;

// In số Fibonacci.
cout << c << endl;

// Cập nhật A và B cho lần lặp tiếp theo.
a = b;
B = C;
}

trả lại 0;
}
`` `

2. ** 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.

Đây là một chương trình trong C ++ kiểm tra xem một số là số nguyên tố:

`` `C ++
#include <Istream>

sử dụng không gian tên STD;

int main () {
// Nhận số để kiểm tra.
int n;
cout << "Nhập một số:";
cin >> n;

// Kiểm tra xem số là số nguyên tố.
bool isprime = true;
for (int i = 2; i <= n / 2; i ++) {
if (n % i == 0) {
isprime = false;
phá vỡ;
}
}

// In kết quả.
if (isprime) {
cout << n << "là một số nguyên tố."<< Endl;
=======================================
#CNH11 #C++ #exercises #Programming #Coding ##CNH 11 C++ Exercises

1. **Write a program to print the first 100 Fibonacci numbers.**

The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding numbers. The first two numbers in the sequence are 0 and 1. So, the first 100 Fibonacci numbers are:

```
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170, 1836311903, 2971215073, 4807526976, 7778742049
```

Here is a program in C++ that prints the first 100 Fibonacci numbers:

```c++
#include <iostream>

using namespace std;

int main() {
// Define the first two Fibonacci numbers.
int a = 0;
int b = 1;

// Print the first 100 Fibonacci numbers.
for (int i = 0; i < 100; i++) {
// Calculate the next Fibonacci number.
int c = a + b;

// Print the Fibonacci number.
cout << c << endl;

// Update a and b for the next iteration.
a = b;
b = c;
}

return 0;
}
```

2. **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.

Here is a program in C++ that checks if a number is prime:

```c++
#include <iostream>

using namespace std;

int main() {
// Get the number to check.
int n;
cout << "Enter a number: ";
cin >> n;

// Check if the number is prime.
bool isPrime = true;
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0) {
isPrime = false;
break;
}
}

// Print the result.
if (isPrime) {
cout << n << " is a prime number." << endl;
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top