CH6_1
#include <stdio.h>
int main(void)
{
double max = 0, n;
do {
printf("Enter a number:");
scanf("%lf", &n);
max = max < n ? n : max;
} while (n > 0.0);
printf("The largest number entered was: %f", max);
return 0;
}
CH6_2
#include <stdio.h>
int main(void)
{
int m, n, t = 0;
printf("Enter two integers: ");
scanf("%d %d", &m, &n);
while (n != 0) {
t = n;
n = m % n;
m = t;
}
printf("Greatest common divisor: %d\n", m);
return 0;
}
CH6_3
#include <stdio.h>
int main(void)
{
int m, n, t;
int M, N;
printf("Enter a fraction: ");
scanf("%d/%d", &m, &n);
if (m == 0) {
printf("In lowest terms: 0\n");
return 0;
}
M = m;
N = n;
while (n != 0) {
t = n;
n = m % n;
m = t;
}
M = M / m;
N = N / m;
printf("In lowest terms: %d/%d\n", M, N);
return 0;
}
CH6_4
#include <stdio.h>
int main(void)
{
double commission, value;
printf("Enter value of trade: ");
scanf("%lf", &value);
while (value != 0.0f) {
printf("Enter value of trade: ");
scanf("%lf", &value);
if (value < 2500.00f)
commission = 30.00f + .017f * value;
else if (value < 6250.00f)
commission = 56.00f + .0066f * value;
else if (value < 20000.00f)
commission = 76.00f + .0034f * value;
else if (value < 50000.00f)
commission = 100.00f + .0022f * value;
else if (value < 500000.00f)
commission = 155.00f + .0011f * value;
else
commission = 255.00f + .0009f * value;
if (commission < 39.00f)
commission = 39.00f;
printf("Commission: $%.2f\n\n", commission);
}
return 0;
}
CH6_5
#include <stdio.h>
int main(void)
{
int num, a, i = 1;
printf("Enter the number: ");
scanf("%d", &num);
do {
a = num / i % 10;
i *= 10;
printf("%d", a);
} while (i < num);
return 0;
}
CH6_6
#include <stdio.h>
int main(void)
{
int i, n;
printf("Enter the number: ");
scanf("%d", &n);
for (i = 2; i * i <= n; i += 2)
printf("%d\n", i * i);
return 0;
}
CH6_7
#include <stdio.h>
int main(void)
{
int n, odd, square;
printf("This program prints a table of squares.\n");
printf("Enter number of entries in table: ");
scanf("%d", &n);
odd = 3;
//for语句中第三句为逗号表达式
for (int i = 1, square = 1; i <= n; printf("%10d%10d\n", i, square), square += odd, odd += 2, ++i);
return 0;
}
CH6_8
#include <stdio.h>
int main(void)
{
int i, n, first;
printf("Enter number of days in month: ");
scanf("%d", &n);
printf("Enter starting day of the week (1=Sun, 7=Sat): ");
scanf("%d", &first);
for (i = 1; i < first; i++)
printf(" ");
/* now print the calendar */
for (i = 1; i <= n; i++) {
printf("%3d", i);
if ((first + i - 1) % 7 == 0)
printf("\n");
}
return 0;
}
CH6_9
#include <stdio.h>
int main(void)
{
double loan, interest_rate, monthly_payment;
int no_of_payments;
printf("Enter amount of loan: ");
scanf("%lf", &loan);
printf("Enter interest rate: ");
scanf("%lf", &interest_rate);
printf("Enter monthly payment: ");
scanf("%lf", &monthly_payment);
printf("Enter number of payments: ");
scanf("%d", &no_of_payments);
double monthly_interest = ((interest_rate / 100) / 12) + 1;
int i;
for (i = 1; i <= no_of_payments; i++) {
loan *= monthly_interest;
loan -= monthly_payment;
printf("Balance remaining after %d month(s): $%.2f\n", i, loan);
}
return 0;
}
CH6_10
#include <stdio.h>
int main(void)
{
int m, d, y;
int m_, d_, y_;
int t;
printf("Enter the date(mm/dd/yy): ");
scanf("%d/%d/%d", &m, &d, &y);
m_ = m;
d_ = d;
y_ = y;
t = m * 31 + d + y * 366;
if (m + d + y == 0) {
printf("ERROR\n");
return 0;
}
for (;;) {
printf("Enter the date(mm/dd/yy): ");
scanf("%d/%d/%d", &m, &d, &y);
if (m + d + y == 0) break;
if (t > m * 31 + d + y * 366) {
m_ = m;
d_ = d;
y_ = y;
t = m * 31 + d + y * 366;
}
}
printf("The earlier date is:%d/%d/%d\n", m_, d_, y_);
return 0;
}
CH6_11
#include <stdio.h>
int main(void)
{
int n, i = 1, d = 1;
double E = 1;
printf("Enter the order:");
scanf("%d", &n);
do {
E = 1.0 / d + E;
d = d * (i + 1);
i++;
} while (i <= n);
printf("The e is nearly %f at the %d order.", E, n);
return 0;
}
CH6_12
#include <stdio.h>
int main(void)
{
int i = 1, d = 1;
double min, E = 1;
printf("Enter the minimum term:");
scanf("%lf", &min);
while (1.0 / d >= min) {
E = 1.0 / d + E;
d = d * (i + 1);
i++;
}
printf("The e is nearly %.20f when the min is %.20f.", E, min);
return 0;
}