100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Class notes

Programa resuelto con recursividad

Rating
-
Sold
-
Pages
2
Uploaded on
01-02-2024
Written in
2023/2024

Programa que plantea la resolución de un problema con recursividad. Muestra el código y la explicación paso a paso del mismo

Institution
Course








Whoops! We can’t load your doc right now. Try again or contact support.

Written for

Institution
Study
Course

Document information

Uploaded on
February 1, 2024
Number of pages
2
Written in
2023/2024
Type
Class notes
Professor(s)
Angélica enciso
Contains
All classes

Subjects

Content preview

Problema:

13. Dada la siguiente serie an=an-1+2n, para n > 0 (para n=0, an=0), diseñar una función recursiva que
calcule el término n de la serie.

Solución:

Crear un programa recursivo que nos pida el término “n” a buscar, el cual es el siguiente:

public class Calcular_n {

public static void main(String[] args) {
// Alrak Studios (2023)
int n;
Scanner scanner = new Scanner(System.in);
System.out.println("Ingrese el término a buscar:");
n = scanner.nextInt();
int r=CS(n);
System.out.println("El termino " + n + " de la serie es: " + r);
}
public static int CS(int n) {
if (n==0) {
return 0;
}else {
int an= CS(n-1);
return (an-1)+2*n;
} //Formula: an = an-1 + 2n
}


Interpretación del programa:

Lo que hace el programa es pedir al usuario el término “n” a buscar. En nuestro caso base tenemos:

if (n==0) {return 0;}

Lo cual significa que si “n” o “an” es igual a 0, entonces nuestro programa terminará.

En caso de nuestra parte recursiva tenemos:

else {
int an= CS(n-1);
return (an-1)+2*n;
}

El cuál crea la variable “an” para guardar la posición en la que se encuentra y sucesivamente realizar la
operación de la fórmula an-1+2n.
$3.49
Get access to the full document:

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached

Get to know the seller
Seller avatar
02benitezriveraalexa

Also available in package deal

Get to know the seller

Seller avatar
02benitezriveraalexa Tecnológico Nacional de Pachuca
Follow You need to be logged in order to follow users or courses
Sold
0
Member since
1 year
Number of followers
0
Documents
12
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions