Factorial and Power
Functions that compute a factorial and a power.
- Level
- Intermediate
- Topic:
- Subprocesses & functions
- Concepts:
- Input & output
- Functions
- Para
Open in Codoritmo
Run, change and debug this example in your browser.
What you'll learn
A Funcion takes parameters and returns the value of the variable written before <-. You can call it inside Escribir just like a built-in PSeInt function.
Code
Funcion resultado <- Factorial(n) Definir resultado, i Como Entero resultado <- 1 Para i <- 2 Hasta n Hacer resultado <- resultado * i FinParaFinFuncionFuncion resultado <- Potencia(base, exponente) Definir resultado Como Real Definir i Como Entero resultado <- 1 Para i <- 1 Hasta exponente Hacer resultado <- resultado * base FinParaFinFuncionAlgoritmo FactorialYPotencia Definir n Como Entero Escribir "Escribe un número:" Leer n Escribir n, "! = ", Factorial(n) Escribir "2 elevado a ", n, " = ", Potencia(2, n) Escribir "Comprobación con el operador ^: ", 2 ^ nFinAlgoritmoFlowchart
Each block in the flowchart matches one instruction in the code.
Sample run
This is what the program shows when it runs. The answers typed by the user are highlighted.
Escribe un número:User answer: 66! = 7202 elevado a 6 = 64Comprobación con el operador ^: 64
Challenge
Write a Sumatoria(n) function that adds the numbers from 1 to n.
Show hint
Copy the shape of Factorial, but start at 0 and add instead of multiplying.
Related examples
Linear vs. Binary Search
Counts the comparisons each search needs on the same sorted list.
IntermediatePrime Numbers
Finds every prime number up to a limit.
IntermediateReusable Validation
Validates the email and age on a sign-up form.
Intermediate