Share c++ kiểm tra số hoàn hảo

doanthanh475

New member
## C ++ Kiểm tra số hoàn hảo

[Liên kết đến một bài viết tham khảo]

Một số hoàn hảo là một số nguyên dương bằng tổng số các giao diện dương của nó, không bao gồm chính số.Ví dụ, 6 là một số hoàn hảo vì các ước số của nó là 1, 2 và 3 và 1 + 2 + 3 = 6.

Để kiểm tra xem một số là một số hoàn hảo trong C ++, bạn có thể sử dụng thuật toán sau:

1. Khai báo một biến `Sum` và khởi tạo nó thành 0.
2. Lặp lại thông qua các số từ 1 đến `n / 2`, trong đó` n` là số bạn đang kiểm tra.
3. Nếu số chia hết cho `i`, hãy thêm` i` vào tổng.
4. Nếu tổng bằng `n`, thì số là một số hoàn hảo.
5. Nếu không, số không phải là một số hoàn hảo.

Dưới đây là một ví dụ về cách sử dụng thuật toán này để kiểm tra xem 6 có phải là một số hoàn hảo không:

`` `C ++
#include <Istream>

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

int main () {
// khai báo một biến để lưu trữ số và khởi tạo nó thành 6.
int n = 6;

// Khai báo một biến để lưu trữ tổng của các ước số và khởi tạo nó thành 0.
int sum = 0;

// lặp qua các số từ 1 đến n / 2.
for (int i = 1; i <= n / 2; i ++) {
// Nếu số chia hết cho i, hãy thêm i vào tổng.
if (n % i == 0) {
tổng += i;
}
}

// Nếu tổng bằng n, thì số là một số hoàn hảo.
if (sum == n) {
cout << "6 là một con số hoàn hảo."<< Endl;
} khác {
cout << "6 không phải là một số hoàn hảo."<< Endl;
}

trả lại 0;
}
`` `

### hashtags

* #C ++
* #Perinf số
* #AlGorithM
* #Toán học
* #Programming
=======================================
## C++ Check Perfect Numbers

[Link to a reference article]

A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. For example, 6 is a perfect number because its divisors are 1, 2, and 3, and 1 + 2 + 3 = 6.

To check if a number is a perfect number in C++, you can use the following algorithm:

1. Declare a variable `sum` and initialize it to 0.
2. Iterate through the numbers from 1 to `n / 2`, where `n` is the number you are checking.
3. If the number is divisible by `i`, add `i` to the sum.
4. If the sum is equal to `n`, then the number is a perfect number.
5. Otherwise, the number is not a perfect number.

Here is an example of how to use this algorithm to check if 6 is a perfect number:

```c++
#include <iostream>

using namespace std;

int main() {
// Declare a variable to store the number and initialize it to 6.
int n = 6;

// Declare a variable to store the sum of the divisors and initialize it to 0.
int sum = 0;

// Iterate through the numbers from 1 to n / 2.
for (int i = 1; i <= n / 2; i++) {
// If the number is divisible by i, add i to the sum.
if (n % i == 0) {
sum += i;
}
}

// If the sum is equal to n, then the number is a perfect number.
if (sum == n) {
cout << "6 is a perfect number." << endl;
} else {
cout << "6 is not a perfect number." << endl;
}

return 0;
}
```

### Hashtags

* #C++
* #Perfect Numbers
* #AlGorithM
* #Math
* #Programming
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top