1.
#include <iostream>
using namespace std;
int main() {
int limite, numero = 0, suma = 0;
// 1. Introducir valor del límite
cout << "Introduce el valor del límite: ";
cin >> limite;
// 4. Repetir hasta que Suma > Límite
while (suma <= limite) {
numero++; // 4.1 Incrementar Numero en 1
suma += numero; // 4.2 Sumar Numero a Suma
}
// 5. Visualizar Numero y Suma
cout << "Numero: " << numero << ", Suma: " << suma << endl;
return 0;
}
2.
#include <iostream>
using namespace std;
int main() {
int i = 1, numero, suma = 0;
while (i <= 10) { // Mientras i sea menor o igual a 10
cout << "Ingrese un número: ";
cin >> numero;
if (numero % 2 == 0) { // Si el número es par
suma += numero; // Sumar el número a la suma total
i++; // Incrementar el contador de números pares
}
}
cout << "La suma es = " << suma << endl;
return 0;
}
3.
#include <iostream>
using namespace std;
double calcularSumatoria(int n) {
double suma = 0.0;
for (int i = 1; i <= n; i++) {
suma += 1.0 / i;
}
return suma;
}
int main() {
int n = 100;
double resultado = calcularSumatoria(n);
cout << "La sumatoria es: " << resultado << endl;
return 0;
}
4.
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main() {
cout << setw(10) << "Numero" << setw(12) << "Cuadrado" << setw(10)
<< "Cubo" << setw(15) << "Raiz cuadrada" << endl;
for (int i = 0; i <= 40; i += 2) {
cout << setw(10) << i
<< setw(12) << i * i
<< setw(10) << i * i * i
<< setw(15) << fixed << setprecision(2) << sqrt(i)
<< endl;
}
return 0;
}
5.
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int votos[4] = {0, 0, 0, 0}; // Arreglo para contar votos de los
candidatos 1-4
int voto, totalVotos = 0;
cout << "Ingrese los votos (finalice con 0): ";
while (true) {
cin >> voto;
if (voto == 0) break; // Termina la entrada si se ingresa 0
if (voto >= 1 && voto <= 4) {
votos[voto - 1]++; // Incrementa el conteo del candidato
correspondiente
totalVotos++;
} else {
cout << "Voto inválido. Solo se permiten 1, 2, 3 o 4." <<
endl;
}
}
// Mostrar resultados
cout << "\nResultados de la elección:\n";
cout << "Candidato\tVotos\tPorcentaje\n";
int ganador = 0;
for (int i = 0; i < 4; i++) {
double porcentaje = (totalVotos > 0) ? (votos[i] * 100.0 /
totalVotos) : 0;
cout << i + 1 << "\t\t" << votos[i] << "\t" << fixed <<
setprecision(2) << porcentaje << "%\n";
if (votos[i] > votos[ganador]) {
ganador = i;
}
}
cout << "\nEl candidato ganador es el número " << (ganador + 1) <<
" con " << votos[ganador] << " votos.\n";
return 0;
}