JavaScript is required

Anh (Chị) hãy cho biết kết quả của đoạn lệnh sau là gì?

1. int main () {

2. int n = 5, i, j, tong;

3. for (i = 2; i <= n; i++) {

4. tong = 1;

5. for (j = 2; j <= i/2; j ++)

6. if ( i % j == 0 ) tong += j;

7. if ( tong == j ) cout << i << “ “;

8. }

9. return 0; 10 . }

A.

4

B.

2 3 5

C.

3 5

D.

Lỗi biên dịch

Trả lời:

Đáp án đúng: A


The code iterates from 2 to n (5). For each number, it calculates the sum of its divisors (excluding 1 and the number itself). It then checks if this sum is equal to one of the divisors found during the inner loop. If it is, it prints the number. The condition `tong == j` is likely intended to check if the sum of the divisors equals the last divisor found. - i = 2: Inner loop doesn't run. tong = 1. No output. - i = 3: Inner loop doesn't run. tong = 1. No output. - i = 4: Inner loop runs for j = 2. tong = 1 + 2 = 3. Condition 3 == 2 is false. No output. - i = 5: Inner loop doesn't run. tong = 1. No output. The code should not produce any output.

Câu hỏi liên quan