Skip to content

Instantly share code, notes, and snippets.

@diazvictor
Last active May 18, 2024 02:40
Show Gist options
  • Save diazvictor/30ae1bd7ae0eb4d562b74c11a16ee117 to your computer and use it in GitHub Desktop.
Save diazvictor/30ae1bd7ae0eb4d562b74c11a16ee117 to your computer and use it in GitHub Desktop.
Este código calcula la suma de una serie aritmética utilizando un ciclo while en C++
#include <iostream>
using namespace std;
int main() {
// Declarar variables
int a = 1; // Primer término
int d = 3; // Diferencia común
int n = 100; // Número de términos
int i = 1; // Contador
double sum = 0; // Variable para la suma total
// Ciclo while para recorrer la serie aritmética
while (i <= n) {
// Calcular el término actual
int term = a + (i - 1) * d;
// Desplegar el término actual
cout << "Termino " << i << ": " << term << endl;
// Actualizar la suma total
sum += term;
// Incrementar el contador
i++;
}
// Desplegar la suma total
cout << "\nSuma total de la serie aritmética: " << sum << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment