Chapter 2
pay0.c
pay0.c
/* File: pay0.c
Programmer: Programmer Name
Date: Current Date
This program calculates the pay for one person, given the hours worked
and rate of pay.
*/
main()
{ /* declarations */
int id_number;
float hours_worked,
rate_of_pay,
pay;
/* print title */
printf("***Pay Calculation***\n\n");
/* initialize variables */
id_number = 123;
hours_worked = 20.0
rate_of_pay = 7.5;
/* calculate pay */
pay = hours_worked * rate_of_pay;
/* print data and results */
printf("ID Number = %d\n", id_number);
printf("Hours Worked = %f, Rate of Pay = %f\n",
hours_worked, rate_of_pay);
prinf("Pay = %f\n", pay);
}
pay1.c
pay1.c
/* File: pay1.c
Programmer: Programmer Name
Date: Current Date
This program calculates the pay for one person with the
hours worked and the rate of pay read in from the keyboard.
*/
main()
{
/* declarations */
int id_number;
float hours_worked,
rate_of_pay,
pay;
/* print title */
printf("***Pay Calculation***\n\n");
/* read data into variables */
printf("Type ID Number: ");
scanf("%d", &id_number);
printf("Hours Worked: ");
scanf("%f", &hours_worked);
printf("Hourly Rate: ");
scanf("%f", &rate_of_pay);
/* calculate results */
pay = hours_worked * rate_of_pay;
/* print data and results */
printf("\nID Number = %d\n", id_number);
printf("Hours Worked = %f, Rate of Pay = %f\n",
hours_worked, rate_of_pay);
printf("Pay = %f\n", pay);
}
pay2.c
pay2.c
/* File: pay2.c
Programmer: Programmer Name
Date: Current Date
This program calculates the pay for one person, given the
hours worked and rate of pay.
*/
#define REG_LIMIT 40.0
#define OT_FACTOR 1.5
main()
{ /* declarations */
int id_number;
float hours_worked,
rate_of_pay,
regular_pay, overtime_pay, total_pay;
/* print title */
printf("***Pay Calculation***\n\n");
/* read data into variables */
printf("Type ID Number: ");
scanf("%d", &id_number);
printf("Hours Worked: ");
scanf("%f", &hours_worked);
printf("Hourly Rate: ");
scanf("%f", &rate_of_pay);
/* calculate results */
if (hours_worked > REG_LIMIT) {
regular_pay = REG_LIMIT * rate_of_pay;
overtime_pay = OT_FACTOR * rate_of_pay *
(hours_worked - REG_LIMIT);
}
else {
regular_pay = hours_worked * rate_of_pay;
overtime_pay = 0.0;
}
total_pay = regular_pay + overtime_pay;
/* print data and results */
printf("\nID Number = %d\n", id_number);
printf("Hours Worked = %f, Rate of Pay = %f\n",
hours_worked, rate_of_pay);
printf("Regular Pay = %f, Overtime Pay = %f\n",
regular_pay, overtime_pay);
printf("Total Pay = %f\n", total_pay);
}
pay3.c
pay3.c
/* File: pay3.c
Programmer: Programmer Name
Date: Current Date
This program reads in hours worked and rate of pay and calculates
the pay for a specified number of persons.
*/
#define REG_LIMIT 40.0
#define OT_FACTOR 1.5
main()
{
/* declarations */
int id_number, count;
float hours_worked, rate_of_pay,
regular_pay, overtime_pay, total_pay;
/* print title */
printf("***Pay Calculation***\n\n");
printf("Number of people: ");
scanf("%d", &count);
while (count > 0) {
/* read data into variables */
printf("\nType ID Number: ");
scanf("%d", &id_number);
printf("Hours Worked: ");
scanf("%f", &hours_worked);
printf("Hourly Rate: ");
scanf("%f", &rate_of_pay);
/* calculate results */
if (hours_worked > REG_LIMIT) {
regular_pay = REG_LIMIT * rate_of_pay;
overtime_pay = OT_FACTOR * rate_of_pay *
(hours_worked - REG_LIMIT);
}
else {
regular_pay = hours_worked * rate_of_pay;
overtime_pay = 0.0;
}
total_pay = regular_pay + overtime_pay;
/* print data and results */
printf("\nID Number = %d\n", id_number);
printf("Hours Worked = %f, Rate of Pay = %f\n",
hours_worked, rate_of_pay);
printf("Regular Pay = %f, Overtime Pay = %f\n",
regular_pay, overtime_pay);
printf("Total Pay = %f\n", total_pay);
/* update the count */
count = count - 1;
}
}
pay4.c
pay4.c
/* File: pay4.c
Programmer: Programmer Name
Date: Current Date
This program reads in hours worked and rate of pay and calculates
the pay for several persons. The program also computes the gross pay
disbursed, number of people, and average pay. The end of data is
signaled by a negative or a zero id number.
*/
#define REG_LIMIT 40.0
#define OT_FACTOR 1.5
main()
{
/* declarations */
int id_number, number;
float hours_worked, rate_of_pay,
regular_pay, overtime_pay, total_pay,
gross, average;
/* print title */
printf("***Pay Calculation***\n\n");
/* initialize cumulative sum variables */
number = 0;
gross = 0;
/* initialize loop variables */
printf("Type ID Number, 0 to quit: ");
scanf("%d", &id_number);
while (id_number > 0) {
/* read data into variables */
printf("Hours Worked: ");
scanf("%f", &hours_worked);
printf("Hourly Rate: ");
scanf("%f", &rate_of_pay);
/* calculate results */
if (hours_worked > REG_LIMIT) {
regular_pay = REG_LIMIT * rate_of_pay;
overtime_pay = OT_FACTOR * rate_of_pay *
(hours_worked - REG_LIMIT);
}
else {
regular_pay = hours_worked * rate_of_pay;
overtime_pay = 0;
}
total_pay = regular_pay + overtime_pay;
/* print data and results */
printf("\nID Number = %d\n", id_number);
printf("Hours Worked = %f, Rate of Pay = $%f\n",
hours_worked, rate_of_pay);
printf("Regular Pay = $%f, Overtime Pay = $%f\n",
regular_pay, overtime_pay);
printf("Total Pay = $%f\n", total_pay);
/* update cumulative sums */
number = number + 1;
gross = gross + total_pay;
/* debug statements, print variable values */
printf("\ndebug: gross = %f, number = %d\n", gross, number);
/* update loop variables */
printf("\nType ID Number, 0 to quit: ");
scanf("%d", &id_number);
}
if (number > 0) {
average = gross / (float) number;
printf("\n***Summary of Payroll***\n");
printf("Number of people = %d, Gross Disbursements = $%f\n",
number, gross);
printf("Average pay = $%f\n", average);
}
}
prime.c
prime.c
/* File: prime.c
Programmer: Programmer Name
Date: Current Date
This program finds all prime numbers less than PRIME_LIM.
*/
#define PRIME_LIM 20
#define TRUE 1
#define FALSE 0
main()
{ int candidate, divisor, prime;
printf("***Prime Numbers Less than %d***\n\n", PRIME_LIM);
printf("%d is a prime number\n", 1); /* print 1 */
candidate = 2; /* start at candidate == 2 */
while (candidate < PRIME_LIM) { /* stop at candidate == 20 */
prime = TRUE; /* for candidate, set prime to True */
divisor = 2; /* initialize divisor to 2 */
/* stop when divisor == candidate */
while (divisor < candidate) {
printf("debug: candidate = %d, divisor = %d prime = %d\n",
candidate, divisor,prime);
/* if candidate is divisible by divisor, */
/* candidate is not prime, set prime to False */
if (candidate % divisor == 0)
prime = FALSE;
divisor = divisor + 1; /* update divisor */
}
if (prime) /* if prime is set to True, */
/* print candidate. */
printf("%d is a prime number\n", candidate);
candidate = candidate + 1; /* update candidate */
}
}