Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
557 B
0
Indexable
Never
//Calcular la siguiente suma
//S = 1 - 2 + 3 - 4 + 5 - ......... + / -N

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
    int i = 0, N = 0;
    float sum = 0, num = 0;
    cout << "Ingrese N: ";
    cin >> N;
    while (i < N)
    {
        i = i + 1;
        if (i % 2 == 0) //Todos los pares restan
        {
            sum = sum - i;
        }
        else //Todos los impares suman
        {
            sum = sum + i;
        }
    }
    cout << "La Suma es: " << sum << endl;

    return 0;
}