Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem 1: Write a Java program that reads an annual cash flow of 10 years and c

ID: 3914290 • Letter: P

Question

Problem 1: Write a Java program that reads an annual cash flow of 10 years and calculates net present value (NPV) of the 10-year cash flows. The program must read an interest rate to calculate the NPV Use loop statement(s) and array(s). cash flow at year-t + (1 + interest rate%)2+ cash flow at year t(1+interest rate%)' NPV 0 (I + interest rate%)' Sample Inputs/ outputs Enter your annual cash flow: 100 Enter an interest rate: 0.01 NPV 947.1305 Important Notes: Assumed that the user makes mistake sat most once while entering the data, i.e. he/she enters the correct value the next time right after being warned

Explanation / Answer

import java.lang.Math;

import java.util.Scanner;

class Main {

// Taking user input

// Giving one more chance to enter correct data

public static double inputData(String prompt)

{

Scanner sc = new Scanner(System.in);

System.out.print(prompt);

double num = sc.nextDouble();

if(num < 0)

{

System.out.print("That is invalid! "+prompt);

num = sc.nextDouble();

if(num < 0)

return -1;

}

return num;

}

public static void main(String[] args) {

// declaring variables and taking user input

double annual = inputData("Enter your annual cash flow: ");

double npv = 0.0;

double interestRate=0.0;

if(annual > 0)

{

interestRate = inputData("Enter an interest rate: ");

if(interestRate > 0)

{

// looping FOR 10 times and calculating the npv

for(int i=0; i<10; i++)

{

npv += annual / Math.pow(1+interestRate, i+1);

}

}

}

// PRINTING OUTPUT

if(annual < 0 || interestRate < 0)

System.out.println("You entered invalid data.");

else

System.out.printf("NPV = %.4f",npv);

}

}

/*SAMPLE OUTPUT

Enter your annual cash flow: 100

Enter an interest rate: 0.01

NPV = 947.1305

*/