write using java programimg Problem 1: Write a Java program that reads an annual
ID: 3915648 • Letter: W
Question
write using java programimg
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 cash flow at year (1 + interest rate%)1 ' (1 + interest rate%)2 cash flow at year (1 + interest rate%) 10 Sample Inputs/ outputs Enter vour 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 warnedExplanation / Answer
As the only input to be validated is the interest as it must be within 0 to 100, hence we put a check in that only. Please use import java.util.Scanner.
Please be very careful while copying/re-writing the code!
//===========================================
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Scanner;
public static void main (String[] args) throws java.lang.Exception
{
double cash,interest,npv=0;
int i;
Scanner in = new Scanner(System.in);
System.out.println("Enter you annual cash flow: ");
cash = in.nextDouble();
System.out.println("Enter an interest rate: ");
interest = in.nextDouble();
while(interest>1 || interest<0)
{
System.out.println("Invalid value for interest, enter again: ");
interest = in.nextFloat();
}
for(i=1;i<=10;i++)
{
npv=npv+(cash/Math.pow(1+interest,i));
}
System.out.println("NPV = "+npv);
}
//=============================================
Sample output:
Enter you annual cash flow: 100
Enter an interest rate: -1
Invalid value for interest, enter again: 2
Invalid value for interest, enter again: 0.01
NPV = 947.1304542057862
Hope this helps!
PLEASE THUMBS UP!!!!!!!!!!!!!!!!!!!!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.