Part C # Calculates the average score using a conditional-controlled while-loop
ID: 3781137 • Letter: P
Question
Part C
# Calculates the average score using a conditional-controlled while-loop
total = 0.0
counter = 0
score = input("Enter a score to average (or -1 when done): ")
while score > 0:
total = total + score
counter = counter + 1
score = input("Enter a score to average (or -1 when done): ")
if numberOfScores > 0:
print "The average score is", total / numberOfScores
Write a program similar to Part C that calculates the amount of money a person would earn over a period of time if their salary is one penny the first day, two pennies the second day, and continues to double each day. This version of the program should determine how many days a person would need to work before they received a million dollars per day in salary.
-PYHTON
Explanation / Answer
import java.util.Scanner;
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
public class PenniesForPay
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
DecimalFormat formatter = new DecimalFormat("#.00");
int numberOfDaysWorked;
double totalPay, salary, pennies;
System.out.println("Enter number of days:");
numberOfDaysWorked = input.nextInt();
while(numberOfDaysWorked < 1)
{
System.out.println("Must enter number greater than 1:");
System.out.println("Enter number of days:");
numberOfDaysWorked = input.nextInt();
}
System.out.println("Day Amount(Dollars)");
System.out.println("--------------------------");
int day = 0;
pennies = 1;
totalPay = 0;
for(int x = 1; x <= numberOfDaysWorked; x++)
{
day++;
if(day > 1)
{
pennies *= 2;
salary = pennies/100;
System.out.println(day + " $ " + formatter.format(salary));
}
else
{
salary = pennies/100;
System.out.println(day + " $ " + formatter.format(salary));
}
totalPay += salary;
}
System.out.println("The total pay is: " + totalPay);
}
}