Last active
May 18, 2024 02:40
-
-
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++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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