-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseccion_4_5_ejercicio.c
More file actions
66 lines (47 loc) · 1.44 KB
/
Copy pathseccion_4_5_ejercicio.c
File metadata and controls
66 lines (47 loc) · 1.44 KB
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
/*
EJERCICIO
Implementa el mismo enfoque (dos funciones) para este problema:
Calcular la potencia base^exponente, con exponente entero >= 0.
- potenciaIterativa(base, exponente); usando un 'for'
- potenciaRecursiva(base, exponente): usando recursión
Prueba con:
base = 2, exponente = 5 -> 32
base = 3, exponente = 0 -> 1
Notas:
- Sin menús
- Sin arreglos ni punteros
- Asume datos válidos
*/
#include <stdio.h>
int potenciaIterativa(int base, int exponente);
int potenciaRecursiva(int base, int exponente);
int main(void) {
int base;
int exponente;
printf("Base (ingresa un número entero (>0)): ");
scanf("%d", &base);
printf("Exponente: (ingresa un número entero (>0)): ");
scanf("%d", &exponente);
// Asumimos entrada válida
int resultado1 = potenciaIterativa(base, exponente);
int resultado2 = potenciaRecursiva(base, exponente);
printf("Potencia iterativa %d^%d = %d\n", base, exponente, resultado1);
printf("Potencia recursiva %d^%d = %d\n", base, exponente, resultado2);
return 0;
}
int potenciaIterativa(int base, int exponente) {
int potencia = 1;
int i;
for (i = 1; i <= exponente; i++) {
potencia *= base;
}
return potencia;
}
int potenciaRecursiva(int base, int exponente) {
// Caso base
if (exponente <= 0) {
return 1; // potenciaRecursiva(base, 0) = 1
}
// Paso recursivo
return base * potenciaRecursiva(base, exponente - 1);
}