Question

# Design a program that calculates the amount of money a person would earn over a period...

Design a program that calculates the amount of money a person would earn over a period of time if his or her salary is one penny the first day, two pennies the second day, and continues to double each day. The program should ask the user for the number of days. Display a table showing what salary was for each day, and then show the total pay at the end of the period. The output should be displayed in a dollar amount, not the number of pennies. Use while loop to code for this particular assignment in JAVA

The following code was programmed using a for loop

package salary;

import java.util.*;

public class Salary {

public static void salaryInfo(int n){

double currentSalary = 0.01;

double totalSalary = 0;

System.out.println("Day\tSalary(DollarAmount)");

for(int i = 1;i <= n;i++){

//Display day number and salary

System.out.println(i+"\t"+currentSalary);

//Add current salary to total salary

totalSalary+=currentSalary;

//double the current salary

currentSalary*=2;

}

//Display total salary

System.out.println("Total Salary\t"+totalSalary);

}

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out.println("Enter number of days : ");

int n = in.nextInt();

if(n>0&&n<=30)

salaryInfo(n);

}

}

Solution:

Note : In the question, it is only asked to use while loop.

Code:

package salary;

import java.util.*;

public class Salary {

public static void salaryInfo(int n){

double currentSalary = 0.01;

double totalSalary = 0;

System.out.println("Day\tSalary(DollarAmount)");

int i=0;

while(i<n){

//Display day number and salary

System.out.println(i+"\t"+currentSalary);

//Add current salary to total salary

totalSalary+=currentSalary;

//double the current salary

currentSalary*=2;

i++; //increment i value

}

//Display total salary

System.out.println("Total Salary\t"+totalSalary);

}

public static void main(String[] args)

{

Scanner in = new Scanner(System.in);

System.out.println("Enter number of days : ");

int n = in.nextInt();

if(n>0&&n<=30)

salaryInfo(n);

}

}

/*

Output :

Enter number of days :

5

Day Salary(DollarAmount)

0 0.01

1 0.02

2 0.04

3 0.08

4 0.16

Total Salary 0.31000000000000005

*/