Problem 2 Write a program that calculates and displays the sum of the integers f
ID: 3724822 • Letter: P
Question
Problem 2 Write a program that calculates and displays the sum of the integers from 1 to a given number. The user inputs an integer number of 2 or more. The program should calculate and display the sum of all of the integers from 1 to this number. (For example, if you input 4 as the number, the program should calculate the sum: 1 + 2 + 3 + 4 and display the answer as 10.) Use a loop to validate the input, i.e. make sure that the number input is 2 or greater before proceeding with the calculation. YOU MUST USE AN ACCUMULATOR.
Sample output (TEST CASE 1):
Test Case 2: 22
Test Case 3: 50
Problem 3: Write a program to see how your money grows in an investment account. You will enter the initial amount deposited, the interest rate, and the number of years the money will remain in the account. Assume no withdrawls are made during this time period. Calculate and display the balance at the end of each year based on the interest rate. Also display the total amount of interest earned. Use loops to validate that the deposit amount, interest rate, and number of years is greater than 0. YOU MUST USE AN ACCUMULATOR.
Sample Output (Test Case 1)
Test case 2: $4900.50 deposited for 10 years at 7% interest
Test case 3: $10000 deposited for 20 years at 5.5% interest
Explanation / Answer
2)
SumOfNumbersFrom1ToN.java
import java.util.Scanner;
public class SumOfNumbersFrom1ToN {
public static void main(String[] args) {
//Declaring variables
int n,sum=0;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
//Getting the input entered by the user
System.out.print("Enter a positive integer:");
while(true)
{
n=sc.nextInt();
if(n<2)
{
System.out.print("Must be 2 or more, re-enter:");
continue;
}
else
break;
}
//Calculating the sum
for(int i=1;i<=n;i++)
{
sum+=i;
}
//Displaying the output
System.out.println("The sum of the integers from 1 to "+n+" is "+sum);
}
}
___________________
Output:
Enter a positive integer:-4
Must be 2 or more, re-enter:1
Must be 2 or more, re-enter:4
The sum of the integers from 1 to 4 is 10
_______________
3)
InvestmentAcc.java
import java.util.Scanner;
public class InvestmentAcc {
public static void main(String[] args) {
//Declaring variables
double initialAmount, rate, totalInterestEarned, amt, firstAmt;
int noOfYears;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
//Getting the input entered by the user
System.out.print("What is initial deposit? ");
while (true) {
initialAmount = sc.nextDouble();
if (initialAmount <= 0) {
System.out.print("Invalid, must be greater than 0. Re-enter:");
continue;
} else
break;
}
System.out.print("What is annual interest rate (as percent)?");
while (true) {
rate = sc.nextDouble();
if (rate <= 0) {
System.out.print("Invalid, must be greater than 0. Re-enter:");
continue;
} else
break;
}
System.out.print("How many years on deposit?");
while (true) {
noOfYears = sc.nextInt();
if (noOfYears <= 0) {
System.out.print("Invalid, must be greater than 0. Re-enter:");
continue;
} else
break;
}
System.out.println(" Let's see how your money grows! ");
firstAmt = initialAmount;
System.out.println("Year Balance");
for (int i = 1; i <= noOfYears; i++) {
amt = initialAmount + initialAmount * (rate / 100);
System.out.printf("%d %.2f ", i, amt);
initialAmount = amt;
}
System.out.printf(" Total interest earned in %d years is $ %.2f ", noOfYears, initialAmount - firstAmt);
}
}
___________________
Output:
What is initial deposit? -1000
Invalid, must be greater than 0. Re-enter:1000
What is annual interest rate (as percent)?0
Invalid, must be greater than 0. Re-enter:10
How many years on deposit?-3
Invalid, must be greater than 0. Re-enter:3
Let's see how your money grows!
Year Balance
1 1100.00
2 1210.00
3 1331.00
Total interest earned in 3 years is $ 331.00
________________
Ouput#2:
What is initial deposit? 4900.50
What is annual interest rate (as percent)?7
How many years on deposit?10
Let's see how your money grows!
Year Balance
1 5243.54
2 5610.58
3 6003.32
4 6423.56
5 6873.20
6 7354.33
7 7869.13
8 8419.97
9 9009.37
10 9640.03
Total interest earned in 10 years is $ 4739.53
__________________
Output#3:
What is initial deposit? 10000
What is annual interest rate (as percent)?5.5
How many years on deposit?20
Let's see how your money grows!
Year Balance
1 10550.00
2 11130.25
3 11742.41
4 12388.25
5 13069.60
6 13788.43
7 14546.79
8 15346.87
9 16190.94
10 17081.44
11 18020.92
12 19012.07
13 20057.74
14 21160.91
15 22324.76
16 23552.63
17 24848.02
18 26214.66
19 27656.47
20 29177.57
Total interest earned in 20 years is $ 19177.57
_______________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.