2011年10月17日月曜日

[CP:AMA] 2 C Fundamentals

Chapter 2: Programing Projects

1.
#include <stdio.h>

int main(void)
{
printf(" *\n");
printf(" *\n");
printf(" *\n");
printf("* *\n");
printf(" * *\n");
printf(" *\n");

return 0;
}


2.
#include <stdio.h>

#define PI 3.14f
#define COEFFICIENT (4.0f / 3.0f)

int main(void)
{
float volume = 0, radius = 0;

printf("Enter the radius of your sphere:");
scanf("%f", &radius);

volume = COEFFICIENT * PI * radius * radius * radius;

printf("The volume of the sphere: %.2f\n", volume);

return 0;
}


4.
#include <stdio.h>

int main(void)
{
float amount = 0;

printf("Enter an amount: ");
scanf("%f", &amount);

printf("With tax added: %0.2f\n", amount * 1.05f);

return 0;
}


5.
#include <stdio.h>

int main(void)
{
float x = 0;

printf("Enter x: ");
scanf("%f", &x);

printf("The answer: %0.2f\n",
3 * x * x * x * x *x +
2 * x * x * x * x -
5 * x * x * x -
x * x +
7 * x -
6);

return 0;
}


6.
#include <stdio.h>

int main(void)
{
float x = 0;

printf("Enter x: ");
scanf("%f", &x);

printf("The answer: %0.2f\n",
((((3 * x + 2) - 5) * x - 1) * x + 7) * x - 6);

return 0;
}


7.
#include <stdio.h>

int main(void)
{
int amount = 0;
int bills20 = 0, bills10 = 0, bills05 = 0, bills01 = 0;

printf("enter a dollar amount: ");
scanf("%d", &amount);

bills20 = amount / 20;
bills10 = (amount - bills20 * 20) / 10;
bills05 = (amount - bills20 * 20 -bills10 * 10) / 5;
bills01 = amount - bills20 * 20 -bills10 * 10 - bills05 * 05;

printf("$20 bills: %d\n", bills20);
printf("$10 bills: %d\n", bills10);
printf(" $5 bills: %d\n", bills05);
printf(" $1 bills: %d\n", bills01);

return 0;
}


8.
#include <stdio.h>

int main(void)
{
float amount_of_loan = 0.0f;
float interest_rate = 0.0f;
float monthly_payment = 0.0f;
float balance_after_first_payment = 0.0f, balance_after_second_payment = 0.0f, balance_after_third_payment = 0.0f;
float monthly_interest_rate = 0.0f;

printf("Enter amount of loan: ");
scanf("%f", &amount_of_loan);
printf("Enter interest rate: ");
scanf("%f", &interest_rate);
printf("Enter monthly payment: ");
scanf("%f", &monthly_payment);

monthly_interest_rate = interest_rate / (100.0f * 12.0f);

amount_of_loan = (amount_of_loan) * (1.0f + monthly_interest_rate);
balance_after_first_payment = amount_of_loan - monthly_payment;
amount_of_loan = (amount_of_loan - monthly_payment) * (1.0f + monthly_interest_rate);
balance_after_second_payment = amount_of_loan - monthly_payment;
amount_of_loan = (amount_of_loan - monthly_payment) * (1.0f + monthly_interest_rate);
balance_after_third_payment = amount_of_loan - monthly_payment;

printf("Balance remaining after first payment: %.2f\n", balance_after_first_payment);
printf("Balance remaining after second payment: %.2f\n", balance_after_second_payment);
printf("Balance remaining after third payment: %.2f\n", balance_after_third_payment);

return 0;
}