Skip to content

Instantly share code, notes, and snippets.

@rszewczyk
Last active April 11, 2017 18:23
Show Gist options
  • Save rszewczyk/ecf45cb2334c62740ea82a861821221f to your computer and use it in GitHub Desktop.
Save rszewczyk/ecf45cb2334c62740ea82a861821221f to your computer and use it in GitHub Desktop.
UMBC CMSC 104 - Homework #3

Homework 3

Part 1

Write a program that prompts the user to enter 2 numbers. If both numbers are even, the program will print the text Both numbers are even. If only the first number is even, the program will print the text The first number is even. If only the second number is even, the program will print the text The second number is even. If neither number is even, the program will print the text Both numbers are odd.

Save your program in a file called hw03part1.c.

Part 2

Write a program that calculates the user's weekly pay. It will prompt the user to enter their standard hourly pay rate, then it will prompt the user to enter the number of hours they worked in the week. Your program will then print the amount that the user will be paid. The user earns their standard hourly rate for the first 40 hours they work in the week. For any time worked in a week more than 40 hours they earn overtime equal to 1.5 times their hourly rate. For example: I worked 55 hours and my standard hourly rate is $20/hour. I will be paid $1250 (40 * 20 + 15 * 30).

Save your program in a file called hw03part2.c.

Submit your work

Make sure that you follow the Coding Standards for both parts.

Submit both files to the hw03 folder.

submit cs104_kdruffel hw03 hw03part1.c hw03part2.c
/*****************************************
** File: part1-solution.c
** Author: Rob Szewczyk
** E-mail: robsz@umbc.edu
**
** This program gets two numbers from the user
** and reports if one, both or none of them
** are even.
**
***********************************************/
#include <stdio.h>
int main() {
int number1;
int number2;
printf("Enter the first number\n");
scanf("%d", &number1);
printf("Enter the second number\n");
scanf("%d", &number2);
int firstIsEven = number1 % 2 == 0;
int secondIsEven = number2 % 2 == 0;
if (firstIsEven && !secondIsEven) {
printf("The first number is even\n");
} else if (!firstIsEven && secondIsEven) {
printf("The second number is even\n");
} else if (firstIsEven && secondIsEven) {
printf("Both numbers are even\n");
} else {
printf("Both numbers are odd\n");
}
return 0;
}
/*****************************************
** File: part2-solution.c
** Author: Rob Szewczyk
** E-mail: robsz@umbc.edu
**
** This program gets the user's hourly pay rate in
** and the number of hours the user worked this
** week. It calulates the total pay based on the
** hourly rate for the first 40 hours worked in
** the week and 1.5 times the hourly rate for time
** over 40 hours.
**
***********************************************/
#include <stdio.h>
int main() {
float hourlyRate;
float hoursWorked;
printf("Enter your hourly pay rate:\n");
scanf("%f", &hourlyRate);
printf("Enter the number of hours you worked in the week\n");
scanf("%f", &hoursWorked);
float totalPay;
if (hoursWorked <= 40.0) {
totalPay = hourlyRate * hoursWorked;
} else {
totalPay = hourlyRate * 1.5 * (hoursWorked - 40.0) + hourlyRate * 40.0;
}
printf("Your pay for the week is: $%.2f\n", totalPay);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment