Dev - C++ - Suma de los numero perfectos y números primos

 
Vista:

Suma de los numero perfectos y números primos

Publicado por Dayana (2 intervenciones) el 02/04/2021 23:11:00
Hola todos espero que todos estén bien, por favor alguien puede ajudarme a entender esos dos programas


UN PROGRAMA QUE LLENE UNA MATRIZ DE FORMA ALEATORIA CON NÚMEROS DEL 1 AL 100 Y UTILIZANDO UNA FUNCIÓN RETORNE LA SUMA DE LOS NUMERO PERFECTOS QUE CONTENGA

UN PROGRAMA QUE LLENE UNA MATRIZ DE FORMA ALEATORIA CON NÚMEROS DEL 1 AL 100 Y UTILIZANDO UNA FUNCIÓN CON PUNTERO RETORNE LA SUMA DE LOS NUMEROS PRIMOS QUE CONTENGA
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
-1
Responder
Imágen de perfil de Alfil
Val: 4.344
Oro
Ha mantenido su posición en Dev - C++ (en relación al último mes)
Gráfica de Dev - C++

Suma de los numero perfectos y números primos

Publicado por Alfil (1444 intervenciones) el 02/04/2021 23:56:20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <cstdlib>
#include <ctime>
 
int random();
void leerMatriz(int **m, int fil, int col);
void imprimirMatriz(int **m, int fil, int col);
bool esPerfecto(int numero);
bool esPrimo(int numero);
 
int main()
{
    int fil, col;
 
    srand(time(NULL));
 
    std::cout << "Filas: "; std::cin >> fil;
    std::cout << "Columnas: "; std::cin >> col;
 
    int** matriz = new int*[fil];
    for (int i = 0; i < fil; i++)
        matriz[i] = new int[col];
 
    leerMatriz(matriz, fil, col);
    imprimirMatriz(matriz, fil, col);
 
    std::cout << std::endl;
 
    return 0;
}
 
int random()
{
    return rand() % 100 + 1;
}
 
void leerMatriz(int **m, int fil, int col)
{
    for (int i = 0; i < fil; i++)
        for (int j = 0; j < col; j++)
            m[i][j] = random();
}
 
void imprimirMatriz(int **m, int fil, int col)
{
    int perfecto = 0, primo = 0;
 
    std::cout << std::endl;
    for (int i = 0; i < fil; i++)
    {
        std::cout << "| ";
        for (int j = 0; j < col; j++)
        {
            std::cout << m[i][j] << " ";
 
            if (esPerfecto(m[i][j])) perfecto++;
            if (esPrimo(m[i][j])) primo++;
        }
        std::cout << "|" << std::endl;
    }
    std::cout << std::endl;
    std::cout << "Numeros Perfectos: " << perfecto << std::endl;
    std::cout << "Numeros Primos: " << primo << std::endl;
}
 
bool esPerfecto(int numero)
{
    int suma = 0;
 
    for (int i = 1; i < numero; i++)
        if (numero % i == 0)
            suma = suma + i;
 
    return suma == numero;
}
 
bool esPrimo(int numero)
{
    int k = 0;
    for(int i = 1; i <= numero; i++)
        if (numero % i == 0)
            k++;
 
    return k == 2;
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Suma de los numero perfectos y números primos

Publicado por Dayana (2 intervenciones) el 03/04/2021 19:44:00
Muchas gracias @Alfil ahora lo entiendo
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar