Purpose: To become familiar with problem solving with loops, Problem A: Pennies
ID: 3599210 • Letter: P
Question
Purpose: To become familiar with problem solving with loops, Problem A: Pennies Per Pay Write a Java program that calculates the amount of 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 display a table showing the salary for each day, and then show the total pay at the end of the period. The output should be displayed in dollar amount, not the number of pennies Prompt and obtain the input value for number of days. Caution: use a no. less than 30 as the total adds to a very big number!) Use Data validation loop to make sure that the valid data for days is> 1 Sample Output: Please Enter the number of daysa6 Here is the Salary for each day: Day Salary (Dollar Amount) 0.01 0.02 0.04 0.08 0.16 0.32 0.63 6 Total Salary Hint: Prompt and read days Dse data validation loop to make aure daya-1 Initialize Tessaltse, total (4o) to o Print the title tor the table Print value of and selarxlocenta/ 100.0 (eonvert to anoune) A total + ealAutoceaSA. // update total eadarztocesaajaxtocensa /ar the and ot the loop /lend of for loop Outside loop: Print the total ,100.0 (convert to amount) with proper label, Print Layout View Sec 1 Pages: 1 of 2 Wa 23Explanation / Answer
Code :
import java.io.*;
import java.util.*;
public class PrintSalary {
public static void printSalaryData(int n){
double currentSalary = 0.01;
double totalSalary = 0;
System.out.println("Day Salary(DollarAmount)");
for(int i = 1;i <= n;i++){
//Display day number and salary
System.out.println(i+" "+currentSalary);
//Add current salary to total salary
totalSalary+=currentSalary;
//double the current salary
currentSalary*=2;
}
//Display total salary
System.out.println("Total Salary "+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)
printSalaryData(n);
}
}
Output :
Enter number of days : 10
Day Salary(DollarAmount)
1 0.01
2 0.02
3 0.04
4 0.08
5 0.16
6 0.32
7 0.64
8 1.28
9 2.56
10 5.12
Total Salary 10.23
**If you have any query , please feel free to comment with details.
**Happy learning :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.