for_chanle

mail@pastecode.io avatar
unknown
c_cpp
a year ago
520 B
3
Indexable
Never
#include <iostream>

using namespace std;

int main()
{
    int n, sumOdd = 0, sumEven = 0;
    cout << "Nhap n = ";
    cin >> n;
   
    for (int i = n; i >= 0; i--) {
        if (i % 2 == 0) {
            sumEven += i;
            cout << i << " ";
        }
    }
    cout << "\nTong chan la: " << sumEven << endl;
    
    for (int i = n; i >= 0; i--) {
        if (i % 2 != 0) {
            sumOdd += i;
            cout << i << " ";
        }
    }
    cout << "\nTong le la: " << sumOdd << endl;
    return 0;
}