Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Use the following link for the worksheet.
- Use this link to see the C precedence chart:
- https://en.cppreference.com/w/c/language/operator_precedence
- PROBLEM #1: Modulo
- Compose a program that prompts the user for an integer value and stores it in a variable.
- Then, use modulo to determine if the number is EVEN or ODD, printing the result to standard output.
- If you are not sure how to approach it, think about this:
- > What MAKES a number even or odd?
- > What number should I use on the right side of the modulus symbol to determine even or odd?
- Problem #2: More Modulo
- Compose a program using modulo to isolate the rightmost digit (least significant digit) of an integer.
- We want to use modulo to get rid of ALL digits but the value in the ones place.
- For example, if the integer is 342556, your goal is to isolate the 6, which is the least significant digit.
- Use the skeleton below to start.
- /** SKELETON CODE **/
- #include <stdio.h>
- int main(void)
- {
- //Local Declarations
- int intNum = 0; //The original number
- int lsDigit = 0; //The least significant digit (You will change this value later)
- //Local Statements
- return 0;
- }//end main
- PROBLEM #3: Division & Yet More Modulo
- Compose a program that uses both division AND modulo to determine the
- quotient and remainder of two integer values provided by the user, then print those two values.
- Use the skeleton code below to start.
- /** SKELETON CODE **/
- #include <stdio.h>
- int main(void)
- {
- int intNum1 = 0; //First number
- int intNum2 = 0; //Second number
- int intQuot = 0; //Quotient
- int intRmdr = 0; //Remainder
- printf("Supply two integer values: ");
- //Scanf here
- //CALCULATE QUOTIENT HERE
- //CALCULATE REMAINDER HERE
- return 0;
- }//end main
- PROBLEM #4: Shifty Business
- Compose a program that declares a defined constant value of 9876543201 called ALONGNBR.
- Now you will manipulate the value to accomplish shifting the decimal point to the right,
- shifting the decimal point to the left, and returning the rightmost values, printing them to output.
- Use the division operator to shift the defined number to the right 4
- places, effectively truncating the "3201" values. Print out this value.
- Then use the modulo operator to return the 2 rightmost values of the original number.
- Print out this value.
- Lastly, use the multiplication operator to shift the original number two places to the left.
- When you print out the last statement your format modifier should use %lld for a
- long long integer.
- SAMPLE OUTPUT:
- Shift 4 places to the left: 987654
- Return 2 rightmost numbers: 1
- Shift 2 places to the right: 987654320100
- PROBLEM #5: Calculate the Hours and Minutes
- Compose a program that prompts the user for an whole number of minutes and store it to a
- variable. Now use division and/or modulo to determine the number of hours and minutes and
- output it to screen.
- For example, if the user enters in 349 minutes, the output should be 5 hours and 49 minutes.
- PROBLEM #6: Tired of Modulo yet?
- Compose a program that will take a floating point number as input (float or double works, just keep it consistent)
- and will round the number to the hundredth place, rounding up. Sample output below.
- So if the user enters in the value 2.345,
- the program will round the number to 2.35 and output it to the screen.
- YOU WILL NEED TO USE SHIFTING (i.e. similar to problem #4) TO DO THIS.
- Hint: The solution provided to me involves moving the decimal point to the left,
- performing a conversion (cast) to int, then shifting the number
- back to the right and converting it back to a floating point data type.
- SAMPLE OUTPUT:
- Enter in a floating point value with at least 3 decimal places.
- 2.325
- The number is now: 2.330000
- NOTE: Printing a value to screen can round the output as its presented, but the actual value in
- memory that is being used for math operations is not rounded, which can cause
- errors hidden by the rounded output.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement