sábado, 16 de noviembre de 2013

Programs Examples from Labs

A metric ton is 35,273.92 ounces. Write a program that will read the weight
of a package of breakfast cereal in ounces and output the weight in metric tons as well
as the number of boxes needed to yield one metric ton of cereal. Your program should
allow the user to repeat this calculation as often as the user wishes.

#include <iostream>
using namespace std;
int main()
{

  char answer;
do 
  {

  const double metric_ton = 35273.92;
  int weight_in_ounces;
  double weight_tons;
  double number_of_boxes;

  cout << "Please enter the weight of a package\n";
  cin >> weight_in_ounces;

  weight_tons = weight_in_ounces / metric_ton;

  number_of_boxes = metric_ton / weight_in_ounces;

  cout << "The weight in metric tons:\n";
  cout << weight_tons<<endl;

  cout << "Number of boxes needed:\n";
  cout << number_of_boxes<<endl;

  cout << "Would you like to do this again?\n";
  cin >> answer;

  }while(answer == 'y' || answer == 'Y');

  return 0;
}


Write a program that accepts a year written as a 4-digit Arabic numeral and
outputs the year written in Roman numerals. Important Roman numerals are V for 5, X
for 10, L for 50, C for 100, D for 500, and M for 1000. Some numbers are formed by using
some kind of subtraction where a lesser-valued numeral is written left of a higher-valued
one. The lesser-valued is then subtracted from the higher-valued one. For example: IV
becomes 4, CM becomes 900, etc. Your program should include a loop that allows the

user play again until he or she is done.

#include <iostream>
using namespace std;
int main()
{
  char ans;
  int yr, m, cm, d, c, xc, l, x, ix, v, i;

  do
    {
      cout << "Enter the Arabic Year: ";
      cin >> yr;
      cout << "Roman Year:  ";
      for(m = 1000; yr >= m; yr -= 1000)
{
 cout << "M";
}
      for(cm = 900; yr >= cm; yr -= 900)
{
 cout << "CM";
}
      for(d = 500; yr >= d; yr -= 500)
{
 cout << "D";
}
      if(yr >= 400)

 cout << "CD"; 
 yr -= 400;
}
      else 
{
 for(c = 100;yr >= c; yr -= 100)
   {
     cout << "C";
   }
}
      for(xc = 90; yr >= xc; yr -= 90)
{
 cout << "XC";
}
      for(l = 50; yr >= l; yr -= 50)
{
 cout << "L";
}
      if(yr >= 40)

 cout << "XL"; yr -= 40; 
}
      else 
{
 for(x = 10; yr >= x; yr -= 10)
   {
     cout << "X";
   }
}
      for(ix = 9; yr >= ix; yr -= 9)
{
 cout << "IX";
}
      for(v = 5; yr >= v; yr -= 5)
{
 cout << "V";
}
      if(yr >= 4)
{  
 cout << "IV"; yr -= 4; 
}
      else {
for(i = 1;yr >= i; yr -= 1)
 {
   cout << "I";
 }
      }

      cout<< "\n\nAgain?[y/n]: ";
      cin>> ans;

    }while(ans == 'y'|| ans == 'Y');

  return 0;



}


Write a program that computes the annual after-tax cost of a new house for
the first year of ownership. The cost is computed as the annual mortgage cost minus the
tax savings. The input should be the price of the house and the down payment. The
annual mortgage cost can be estimated as 3% of the initial loan balance credited toward
paying off the loan principal plus 6% of the initial loan balance in interest. The initial
loan balance is the price minus the down payment. Assume a 35% marginal tax rate
and assume that interest payments are tax deductible. So, the tax savings is 35% of
the interest payment. Your program should use at least two function definitions. Your
program should allow the user to repeat this calculation as often as the user wishes.

#include <iostream>
using namespace std;

double annual_mortgage_cost(double initial_balance);
double tax_savings(double initial_balance);
double initial_loan_balance(double house_price, double down_payment);

int main()
{
  
  double house_price, down_payment, initial_balance;
  char ans;

  cout.setf(ios::fixed);
  cout.setf(ios::showpoint);
  cout.precision(2);


do
  {
    cout << "How much did your house cost?\n";
    cin >> house_price;
    cout << "What was your down payment?\n";
    cin >> down_payment;

    initial_balance = initial_loan_balance(house_price, down_payment);

    cout << "Your annual after-tax cost of home ownership is $" <<annual_mortgage_cost(initial_balance) - tax_savings(initial_balance)<< "\n";

    cout << "Your initial loan balance is " <<initial_balance<< "\n";
    cout << "Thats $" <<annual_mortgage_cost(initial_balance)<< " annual mortgage cost\n";
    cout << "With a tax savings each year of $" <<tax_savings(initial_balance)<< "\n";
    
    cout << "Wanna repeat it again? (y/n)\n";
    cin >> ans;
  }
 while (ans == 'y' || ans == 'Y');

 return 0;
}

double annual_mortgage_cost(double initial_loan_balance)
{
  double cost;
  cost = initial_loan_balance * (.09); //3% credited toward paying off the loan principal and 6% interest
  return cost;
}

double tax_savings (double initial_loan_balance)
{
  double tax_savings;
  tax_savings = initial_loan_balance * 0.021; //35% of a 6% interest payment
  return tax_savings;
}

double initial_loan_balance(double house_price, double down_payment)
{
  double initial_loan_balance;
  initial_loan_balance = house_price - down_payment;
  return initial_loan_balance;
}








No hay comentarios.:

Publicar un comentario