1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | /* Created by nguyenvanhieu.vn */ /* Tinh tong tat ca cac uoc so cua so nguyen duong n */ #include <iostream> using namespace std; int main(){ int n; do{ cin >> n; if(n <= 0){ cout << "\nNhap lai n = "; } }while(n <= 0); int sum = 0; for(int i = 1;i <= n;i++){ if(n % i == 0){ sum += i; } } cout << "\nSum = " << sum << '\n'; } |
Output:
0 1 2 3 | Nhap n = 24 Sum = 60 |