Skip to content

Instantly share code, notes, and snippets.

@Gzoref
Created November 29, 2018 04:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gzoref/4ae3ecd191f63a7e796c74dc3a7f9eda to your computer and use it in GitHub Desktop.
Save Gzoref/4ae3ecd191f63a7e796c74dc3a7f9eda to your computer and use it in GitHub Desktop.
Pennies For Days
/*
* Name: Geoffrey Zoref
* Date: 10/10/2018
* Project: Classes and Objects - Ch. 6
*/
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Scanner;
public class Pennies {
public static void main(String[] args) {
// Declare var to store number of days worked
int numDays;
// User salary
double salary = 0.01;
double totalSalary = 0.01;
// Create scanner object
NumberFormat df;
try (Scanner keyboard = new Scanner(System.in)) {
// Decimal formating for salary variables
df = DecimalFormat.getInstance();
df.setMaximumFractionDigits(2);
// Prompt user for input
System.out.print("Enter the number of days worked: ");
numDays = keyboard.nextInt();
// Input validation: Input must not be less than 1
while (numDays < 1) {
System.out.print("Enter the number of days worked: ");
numDays = keyboard.nextInt();
}
}
// Display table header
System.out.println("Day " + " Salary " + "Total Salary");
// Display number of days
int day = 1;
// Construct while loop to display days, daily salary, and total salary.
while (numDays > 0) {
System.out.println(day + " $" + salary + " $" + df.format(totalSalary));
salary *= 2;
totalSalary += salary;
day++;
numDays--;
}
}
}
/**
* Write a program that calculates how much 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 a dollar amount, not the number of pennies.
* <p>
* Input Validation: Do not accept a number less than one for the number of days worked
* <p>
* n(n+1)/2
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment