Question

In: Computer Science

Write a program that calculates the amount a person would earn over a period of time...

Write a program that calculates the amount 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. Input Validation: Do not accept a number less than 1 for the number of days worked.

Basic Java language please

Solutions

Expert Solution

Have a look at the below code. I have put comments wherever required for better understanding.

import java.util.*;

class Main {
  public static void main(String[] args) {
    // create scanner object to take user input
    Scanner sc = new Scanner(System.in);
    // create variable to store number of days
    int days; 
    System.out.println("Enter the total number of days:");
    // Take input with proper validation
    while (true){
      days = sc.nextInt();
      // if user input is less than 1, give proper message and take input again
      if(days<1){

        System.out.println("Please enter a number greater or equal to 1");
      }
      else{
      // if input is greater or equal to 1 then proceed 
      // create an array to store salary
      double[] table = new double[days];
      // intialize day 1 salary as 1 penny
      int n = 1; 
      // loop in through the days
      for (int i=0;i<days;i++){
        // conver pennies to dollar
        table[i] = n/100.0;
        // double the salary
        n*=2;
      }
      // display the result
      for (int i=0;i<days;i++){
        System.out.print("$");
        System.out.print(table[i]);
        System.out.print("\t");


      }
      break;
    }
    }
    
  }
}

Happy Learning!


Related Solutions

Design a c++ program that calculates the amount of money a person would earn over a...
Design a c++ program that calculates the amount of money a person would earn over a period of time if his salary is one penny the first day, two pennies second day, and continues to double each day. The program should ask the user for the number of days. Display a table showing what the salary was for each day, and then show total pay at the end of the period. the output should be displayed in a dollar amount,...
Your task is to write a program in C or C++ that calculates the total amount...
Your task is to write a program in C or C++ that calculates the total amount of money a person has made over the last year. Your program will prompt the user for dollar amounts showing how much the user has made per job. Some users will have had more than one job, make sure your program allows for this. The program will print out the total made (all jobs) and will print out the federal and state taxes based...
Write a program that prompts user to enter integers one at a time and then calculates...
Write a program that prompts user to enter integers one at a time and then calculates and displays the average of numbers entered. Use a while loop and tell user that they can enter a non-zero number to continue or zero to terminate the loop. (Switch statement) Write a program that prompts user to enter two numbers x and y, and then prompts a short menu with following 4 arithmetic operations: Chose 1 for addition Chose 2 for subtraction Chose...
Hello, I am attempting to write a program which calculates the amount of fence pieces needed...
Hello, I am attempting to write a program which calculates the amount of fence pieces needed to achieve a certain distance. The program should take in the amount of fencing needed from the user, then ask them the size of the smaller piece. The fencing is to be made up of two sizes of fencing, the smaller being two feet less than the larger. Then, the program will tell the user how many of each piece is needed. However, I...
Please use Phyton to write a program: Write a program that calculates and displays the total...
Please use Phyton to write a program: Write a program that calculates and displays the total bill at a restaurant for a couple that is dining. The program should collect from the couple, cost of each meal, and the percentage of the final cost that they would like to tip. The sales tax in the state where the restaurant exists is 7.5%. Display to the user, line by line: Total Cost of Both Meals Sales Tax in dollars Tip in...
Discuss why a declining margin of safety over a period of time would be an issue...
Discuss why a declining margin of safety over a period of time would be an issue of concern to managers?
Write a program in python to calculate the amount each person must pay toward the bill...
Write a program in python to calculate the amount each person must pay toward the bill and toward the tip, for a group of friends who are eating out together. Since you are all friends, it is okay to split the costs evenly. Your program should take as input: The restaurant bill (without tax or tip) as a floating point number The sales tax rate as a floating point number (for example: an 8% tax rate would be 0.08) The...
write a program to calculate and print payslips write program that calculates and prints payslips. User...
write a program to calculate and print payslips write program that calculates and prints payslips. User inputs are the name of employee, numbers of hours worked and hourly rate c++ language
Description: The purpose of this assignment is to write code that calculates the amount of interest...
Description: The purpose of this assignment is to write code that calculates the amount of interest accrued on a credit card on its expenses. You will create a C program that prompts the user for the total amount of purchases on the credit card, an annual percentage rate (APR), and the number of days the money is borrowed. The program will calculate the amount of the compound interest for the purchases, then print the original amount of purchases, the amount...
Modify the provided code to create a program that calculates the amount of change given to...
Modify the provided code to create a program that calculates the amount of change given to a customer based on their total. The program prompts the user to enter an item choice, quantity, and payment amount. Use three functions: • bool isValidChoice(char) – Takes the user choice as an argument, and returns true if it is a valid selection. Otherwise it returns false. • float calcTotal(int, float) – Takes the item cost and the quantity as arguments. Calculates the subtotal,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT