CH5_1
#include <stdio.h>
int main(void)
{
int num, a;
printf("Enter a number: ");
scanf("%d", &num);
if (num / 1000 > 0) {
a = 4;
}
else if (num / 100 > 0) {
a = 3;
}
else if (num / 10 > 0) {
a = 2;
}
else if (num > 0) {
a = 1;
}
else a = 0;
printf("The number %d has %d digits\n", num, a);
return 0;
}
CH5_2
#include <stdio.h>
int main(void)
{
int clk24h, clkm, clk12h, minute;
printf("Enter the 24_clock(xx:xx): ");
scanf("%d:%d", &clk24h, &clkm);
minute = clk24h * 60 + clkm;
if (minute > 720) {
minute -= 720;
clk12h = minute / 60;
printf("The 12_clock is %d:%.2dPM\n", clk12h, clkm);
}
else {
printf("The 12_clock is %d:%.2dAM\n", clk24h, clkm);
}
return 0;
}
CH5_3
#include <stdio.h>
int main(void)
{
int shares, price_per_share, value;
double broker_commission, rival_commission;
printf("Enter number of shares: ");
scanf("%d", &shares);
printf("Enter price per share: ");
scanf("%d", &price_per_share);
value = shares * price_per_share;
if (shares < 2000)
rival_commission = 33.00 + (shares * 0.03);
else
rival_commission = 33.00 + (shares * 0.02);
if (value < 2500.00f)
broker_commission = 30.00 + 0.017 * value;
else if (value < 6250.00f)
broker_commission = 56.00 + 0.0066 * value;
else if (value < 20000.00f)
broker_commission = 76.00 + 0.0034 * value;
else if (value < 50000.00f)
broker_commission = 100.00 + 0.0022 * value;
else if (value < 500000.00f)
broker_commission = 155.00 + 0.0011 * value;
else
broker_commission = 255.00 + 0.0009 * value;
if (broker_commission < 39.00) {
broker_commission = 39.00;
}
printf("Rivals commission: $%.2f\nBrokers commission: $%.2f\n", rival_commission, broker_commission);
return 0;
}
CH5_4
#include <stdio.h>
int main(void)
{
double speed;
printf("Enter a wind speed: ");
scanf("%lf", &speed);
if (speed < 1.0)
printf("Calm\n");
else if (speed <= 3.0)
printf("Light air\n");
else if (speed <= 27.0)
printf("Breeze\n");
else if (speed <= 47.0)
printf("Gale\n");
else if (speed <= 63.0)
printf("Storm\n");
else
printf("Hurricane\n");
return 0;
}
CH5_5
#include <stdio.h>
int main(void)
{
double income, tax_due;
printf("Enter the income: ");
scanf("%lf", &income);
if (income <= 750)
tax_due = income * 0.01;
else if (income <= 2250)
tax_due = 7.50 + ((income - 750) * 0.02);
else if (income <= 3750)
tax_due = 37.50 + ((income - 2250) * 0.03);
else if (income <= 5250)
tax_due = 82.50 + ((income - 3750) * 0.04);
else if (income <= 7000)
tax_due = 142.50 + ((income - 5250) * 0.05);
else
tax_due = 230.00 + ((income - 7000) * 0.06);
printf("Tax due: $%.2f", tax_due);
return 0;
}
CH5_6
#include <stdio.h>
int main(void)
{
int check_digit, d, i1, i2, i3, i4, i5, j1, j2, j3, j4, j5,
first_sum, second_sum, total;
printf("Enter the first (single) digit: ");
scanf("%1d", &d);
printf("Enter first group of five digits: ");
scanf("%1d%1d%1d%1d%1d", &i1, &i2, &i3, &i4, &i5);
printf("Enter second group of five digits: ");
scanf("%1d%1d%1d%1d%1d", &j1, &j2, &j3, &j4, &j5);
printf("Enter the last digit: ");
scanf("%1d", &check_digit);
first_sum = d + i2 + i4 + j1 + j3 + j5;
second_sum = i1 + i3 + i5 + j2 + j4;
total = 3 * first_sum + second_sum;
if (check_digit == 9 - ((total - 1) % 10))
printf("VALID\n");
else
printf("NOT VALID\n");
return 0;
}
CH5_7
#include <stdio.h>
int main(void)
{
int a, b, c, d, max, min;
printf("Enter four integers: ");
scanf("%d%d%d%d", &a, &b, &c, &d);
if (a >= b) {
max = a;
min = b;
}
else {
max = b;
min = a;
}
if (c > max) {
max = c;
}
else if (c < min) {
min = c;
}
if (d > max) {
max = d;
}
else if (d < min) {
min = d;
}
printf("Largest integer: %d\nSmallest integer: %d\n", max, min);
return 0;
}
CH5_8
#include <stdio.h>
int main(void)
{
int hour, minute, mins;
printf("Enter a 24-hour time: ");
scanf("%d:%d", &hour, &minute);
mins = (hour * 60) + minute;
if (mins <= ((8 * 60) + (103 / 2)))
printf("Closest departure time is 8:00 AM., arriving at 10:16 AM");
else if (mins < ((9 * 60) + 43) + (96 / 2))
printf("Closest departure time is 9:43 AM., arriving at 11:52 AM");
else if (mins < ((11 * 60) + 19) + (88 / 2))
printf("Closest departure time is 11:19 AM., arriving at 1:31 PM");
else if (mins <= ((12 * 60) + 47) + (73 / 2))
printf("Closest departure time is 12:47 PM., arriving at 3:00 PM");
else if (mins <= ((14 * 60) + (105 / 2)))
printf("Closest departure time is 2:00 PM., arriving at 4:08 PM");
else if (mins <= ((15 * 60) + 45) + (195 / 2))
printf("Closest departure time is 3:45 PM., arriving at 5:55 PM");
else if (mins <= ((19 * 60) + (165 / 2)))
printf("Closest departure time is 7:00 PM., arriving at 9:20 PM");
else
printf("Closest departure time is 9:45 PM., arriving at 11:58 PM");
return 0;
}
CH5_9
#include <stdio.h>
int main(void)
{
int m1, d1, y1;
int m2, d2, y2;
int t1, t2;
printf("Enter first date(mm/dd/yy): ");
scanf("%d/%d/%d", &m1, &d1, &y1);
printf("Enter second date(mm/dd/yy): ");
scanf("%d/%d/%d", &m2, &d2, &y2);
t1 = y1 * 366 + m1 * 31 + d1;
t2 = y2 * 366 + m2 * 31 + d2;
if (t1 < t2) {
printf("The ealier date is: %d/%d/%d\n", m1, d1, y1);
}
else if (t1 > t2) {
printf("The ealier date is: %d/%d/%d\n", m2, d2, y2);
}
else {
printf("The are the same date.");
}
return 0;
}
CH5_10
#include <stdio.h>
int main(void)
{
int grade;
printf("Enter the grade: ");
scanf("%d", &grade);
switch (grade / 10) {
case 10: if (grade % 10 == 0) printf("Letter grade: A\n"); break;
case 9: printf("Letter grade: A\n"); break;
case 8: printf("Letter grade: B\n"); break;
case 7: printf("Letter grade: C\n"); break;
case 6: printf("Letter grade: D\n"); break;
case 5:
case 4:
case 3:
case 2:
case 1: printf("Letter grade: F\n"); break;
case 0: if (grade % 10 >= 0) printf("Letter grade: F\n"); break;
default: printf("ERROR\n"); break;
}
return 0;
}
CH5_11
#include <stdio.h>
int main(void)
{
int number, tens, ones;
printf("Enter a two-digit number: ");
scanf("%d", &number);
if (number < 10 || number > 99) {
printf("ERROR\n");
return 0;
}
tens = number / 10;
ones = number % 10;
printf("You entered the number ");
if (tens == 1) {
switch (ones) {
case 0: printf("ten.");
break;
case 1: printf("eleven.");
break;
case 2: printf("twelve.");
break;
case 3: printf("thirteen.");
break;
case 4: printf("fourteen.");
break;
case 5: printf("fifteen.");
break;
case 6: printf("sixteen.");
break;
case 7: printf("seventeen.");
break;
case 8: printf("eighteen.");
break;
case 9: printf("nineteen.");
break;
}
}
else {
switch (tens) {
case 2: printf("twenty-");
break;
case 3: printf("thirty-");
break;
case 4: printf("forty-");
break;
case 5: printf("fifty-");
break;
case 6: printf("sixty-");
break;
case 7: printf("seventy-");
break;
case 8: printf("eighty-");
break;
case 9: printf("ninety-");
break;
}
switch (ones) {
case 1: printf("one.");
break;
case 2: printf("two.");
break;
case 3: printf("three.");
break;
case 4: printf("four.");
break;
case 5: printf("five.");
break;
case 6: printf("six.");
break;
case 7: printf("seven.");
break;
case 8: printf("eight.");
break;
case 9: printf("nine.");
break;
}
}
return 0;
}