0% encontró este documento útil (0 votos)
12 vistas4 páginas

Programa C++ para Almacenar Números Primos

Cargado por

Mariela
Derechos de autor
© © All Rights Reserved
Nos tomamos en serio los derechos de los contenidos. Si sospechas que se trata de tu contenido, reclámalo aquí.
Formatos disponibles
Descarga como PDF, TXT o lee en línea desde Scribd
0% encontró este documento útil (0 votos)
12 vistas4 páginas

Programa C++ para Almacenar Números Primos

Cargado por

Mariela
Derechos de autor
© © All Rights Reserved
Nos tomamos en serio los derechos de los contenidos. Si sospechas que se trata de tu contenido, reclámalo aquí.
Formatos disponibles
Descarga como PDF, TXT o lee en línea desde Scribd

UNIVERSIDAD NACIONAL DE CHIMBORAZO

FACULTAD DE INGENIERIA
PROGRAMACION

NOMBRE: Melanie Llumiquinga


CURSO: PRIMER SEMESTRE “B”
MATERIA: PROGRAMACION
INGENIERA: MARIA ISABEL UVIDIA
FECHA:2023/12/18
TEMA: VECTORES
1) REALIZAR UN PROGRAMA EN C++, QUE ME PERMITA ALMACENAR
N ELEMENTOS EN UN [Link] SE ALMACENARA LOS
NUMEROS QUE SEAN PRIMOS, CASO CONTRARIO SE
ALAMACENARA EL NUMERO [Link] DETERMINAR SI UN
NUMERO ES PRIMO, SE DEBE UTILIZAR UNA FUNCION

Código.
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
bool esPrimo(int num) {
if (num <= 1) {
return false;
}
for (int i = 2; i <= sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
UNIVERSIDAD NACIONAL DE CHIMBORAZO
FACULTAD DE INGENIERIA
PROGRAMACION
int main() {
int n;
cout << "Ingrese la cantidad de numeros: ";
cin >> n;
vector<int> numPrimos;
for (int i = 0; i < n; i++) {
int num;
cout << "Ingrese el numero " << i + 1 << ": ";
cin >> num;
if (esPrimo(num)) {
numPrimos.push_back(num);
} else {
numPrimos.push_back(100);
}
}
cout << "Numeros almacenados en el vector: ";
for (int num : numPrimos) {
cout << num << " ";
}
Return 0;
UNIVERSIDAD NACIONAL DE CHIMBORAZO
FACULTAD DE INGENIERIA
PROGRAMACION
UNIVERSIDAD NACIONAL DE CHIMBORAZO
FACULTAD DE INGENIERIA
PROGRAMACION

También podría gustarte