recursive calculation of funds in a bank account

public static double calculateFinalCapital(double initialCapital, int years, double interestRate, double yearlyRate) {
    double finalCapital;
    if(years < 0)
    {   
        return initialCapital;
    }
    else
    return calculateFinalCapital(initialCapital, years-1 ,interestRate ,yearlyRate)*(interestRate+1);
}

Hey everyone, I'm having trouble getting the correct balance to show up :/

 initialCapital is the starting capital
 years is the runtime (in years)
 interestRate is the rate of interest
 yearlyRate is the yearly rate  

Answer №1

It appears that the issue lies in the base condition you have set. Instead of running the recursion back to year -1, it should be set to run back to year 0. Perhaps consider using:

if (n <= 0)

...

Test your code with a time period of 0: the result should return the original principal amount.

Answer №2

public static double calculateCapitalAfterYears(double initialCapital, int years, double interestRate, double yearlyAdditions) {

    //double newCapital;
    if(years <= 0)
    {   
        return initialCapital;
    }
    else
    return calculateCapitalAfterYears(initialCapital, years - 1, interestRate, yearlyAdditions)* (interestRate + 1) + yearlyAdditions;
}

Alright, after a closer look, it turns out I needed to include the + yearlyAdditions in the calculation.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Extract the list of selected item Id's from the HTML table and transfer them to the Controller

I have a table in my ASP.NET MVC project that displays information about file types and IDs (which are hidden) to the user. When the user selects checkboxes and clicks on the "Send Request Mail" button, I need to retrieve the IDs of the selected rows and ...

Generating pop-up upon loading with CSS3

I have searched through numerous threads and forums in the hopes of finding a solution, but I haven't been successful. My issue lies with triggering a popup on my website. I followed online tutorials to create a popup window, which I was able to do su ...

What could be causing jQuery animate to malfunction on mobile devices when a viewport is present?

Everything seems to be working fine on my desktop webpage, but when I try it on mobile, there is no scroll... $("HTML, BODY").animate({ scrollTop: 500 }, 1000); This post suggests that mobile devices may not scroll on the body, but on the vi ...

What steps do I need to take to bring this into my JavaScript code?

I am facing an issue with a JavaScript function that runs in an HTML file, as I keep getting the error "angular is not defined". To address this, I included the following script tag before my HTML script: <script src="https://cdnjs.cloudflare.com/ajax/ ...

"Exploring the Possibilities of AngularJS with JSON

Here is some JSON data that I am working with: { "_id" : ObjectId("542e65368a1cec1227ae2bac"), "result" : { "full" : { "Array1" : [ "mytext1", ...

Simple steps for importing JS files in a web application

Upon examining the code snippet below: const express = require('express'); const app = express(); const http = require('http').Server(app); const io = require('socket.io')(http); const cors = require('cors'); app.u ...

Combining PHP sessions with Vue and Node.js

Currently, I have a PHP application that handles user authentication, MySQL queries, and relies on the $_SESSION cookie for session management. Despite everything working smoothly, I realized the need to enhance the frontend of my application, prompting t ...

Downloading files from Dropbox with the power of Ajax

Greetings, I am attempting to retrieve a specific file from Dropbox using AJAX. The initial response in the console was XHR finished loading: GET "https://content.dropboxapi.com/2/files/download". However, the subsequent response looked like this: %PDF-1 ...

A guide on protruding a specific triangle within a mesh using three.js

Description: In my three.js project, I have implemented a selection tool that allows users to click and drag over the mesh to color the triangles red. Now, I am looking to create a function that will examine the mesh.geometry.attributes and extrude any tr ...

Having difficulty transitioning a function with a promise from JavaScript to TypeScript

I am currently in the process of transitioning my existing NodeJS JavaScript code to TypeScript for a NodeJS base, which will then be converted back to JavaScript when running on NodeJS. This approach helps me maintain clear types and utilize additional fe ...

Using AngularJS, you can pass serialized objects as query string parameters

I'm trying to pass nested objects as query parameters from my AngularJS Controller using the following code: $scope.redirect = function () { const params = $httpParamSerializer({ initval: { user: { id: 1, ...

Angular ng-if directive contains a specific class

I am trying to create a directive that will only be active when an element has a specific class. I have tried the following code: <feedback-analytics ng-if="$('#analyticTab').hasClass('active')"></feedback-analytics> Unfor ...

Encountering an error known as 'Uncaught Promise Rejection'

I encountered an issue while trying to create a live tracking feature on my MapBox map. The error seems to be related to using the same API elsewhere in the file. I suspect this is causing the problem. Is there a way to resolve this issue? Error - Error: ...

What is the process of incorporating root context into a Spring Boot application, excluding the /login endpoint?

Currently, I am developing a Spring Boot application that utilizes JSP as its user interface. One issue I have encountered is the need to establish a root-context /applicationName for the application as a whole, excluding the /login page. To address this, ...

Can JavaScript be used to retrieve values of items in neighboring table rows?

I currently have items displayed in a table setup like this: <td></td> <td><input class="form-control" value="$title"></td> <td><textarea name="Text1" cols="40" rows="5">$tdesc</textarea><td> <td> ...

Executing a .jar file through command-line interface

I am facing some issues while trying to export a runnable .jar file: "VM arguments will not be part of the runnable JAR. Arguments can be passed on the command line when launching the JAR" Despite the warning, I proceeded and finished creating the runnab ...

Adding elements to an array when a div is clicked

I am attempting to display an array containing the IDs of div elements when a user clicks on one of the divs. However, my current code keeps replacing the existing values in the array instead of adding to it. As a result, I only get one, two, or three as o ...

Tips for uploading images, like photos, to an iOS application using Appium

I am a beginner in the world of appium automation. Currently, I am attempting to automate an iOS native app using the following stack: appium-webdriverio-javascript-jasmine. Here is some information about my environment: Appium Desktop APP version (or ...

Error message: "The term 'Outlet' is not recognized in react-router version 6.4"

What is the proper way to define the Outlet component in react-router version 6.4? Below is a code snippet: function Layout() { return ( <div> <Navbar /> <Outlet /> <Footer /> </div> ); } ...

How can I align each JPanel to either the left or right within the main JPanel?

I want my app to look like this: I tried using jpanel.setAlignmentX(JPanel.LEFT_ALIGNMENT); jpanel.setAlignmentX(JPanel.RIGHT_ALIGNMENT); It's working for the first jpanel when I add it to the main_JPanel. Showing correctly But when I add JPanel2, ...