Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PROBLEM #1 (Precedence)
- Given the expressions below, add parentheses to determine the order in which operations will be carried out in the
- expressions. Code these in a code editor and see if your predictions hold true!
- Use this link to see the C precedence chart:
- https://en.cppreference.com/w/c/language/operator_precedence
- 1. 3 * 8 / 4 % 4 * 5
- 2. a += b *= c ‐= 5
- 3. true && false || false
- PROBLEM #2 (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 the remainder is 0, it is even. If the remainder is anything BUT 0, it is odd).
- Problem #3 (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.
- (Hint if we remove the tens place, we also remove anything above it, leaving the ones place value.)
- /** SKELETON CODE **/
- #include <stdio.h>
- int main(void)
- {
- //Local Declarations
- int intNum = 0;
- int lsDigit = 0;
- //Local Statements
- return 0;
- }//end main
- PROBLEM #4 (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 at runtime.
- Then print those two values.
- /** SKELETON CODE **/
- #include <stdio.h>
- int main(void)
- {
- //Local declarations
- int intNum1 = 0;
- int intNum2 = 0;
- int intCalc = 0;
- //Local Statements
- printf("Supply two integral values: ");
- //Scanf here
- //CALCULATE QUOTIENT HERE
- //CALCULATE ANY REMAINDER HERE
- return 0;
- }//end main
- PROBLEM #5 (Shifty Business)
- Compose a program that declares a defined constant value of 9876543201 called ALONGNBR.
- Now you will manipulate the value to accomplish shifting to the right,
- the left and to return the rightmost values, printing them to output.
- You will make a printf() statement that will use the division operator to shift the defined number to the right 4
- places, effectively truncating the "3201" values.
- You will then make a second printf() statement that should use the
- modulo operator to return the 2 rightmost values of the original number.
- Lastly you will make a printf() statement that should 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 #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 of accuracy, 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 #5) TO DO THIS.
- Hint: The solution involves moving the decimal point to the right,
- 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.
Add Comment
Please, Sign In to add comment