Advertisement
M4ritimeSeeker

PASS Week 4: Operators, Operands, and Precedence

Feb 8th, 2022 (edited)
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 KB | None | 0 0
  1. Use the following link for the worksheet.
  2. Use this link to see the C precedence chart:
  3.  
  4. https://en.cppreference.com/w/c/language/operator_precedence
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35. PROBLEM #1: Modulo
  36.  
  37. Compose a program that prompts the user for an integer value and stores it in a variable.
  38. Then, use modulo to determine if the number is EVEN or ODD, printing the result to standard output.
  39. If you are not sure how to approach it, think about this:
  40. > What MAKES a number even or odd?
  41. > What number should I use on the right side of the modulus symbol to determine even or odd?
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58. Problem #2: More Modulo
  59.  
  60. Compose a program using modulo to isolate the rightmost digit (least significant digit) of an integer.
  61. We want to use modulo to get rid of ALL digits but the value in the ones place.
  62. For example, if the integer is 342556, your goal is to isolate the 6, which is the least significant digit.
  63. Use the skeleton below to start.
  64.  
  65.  
  66. /** SKELETON CODE **/
  67.  
  68. #include <stdio.h>
  69.  
  70. int main(void)
  71. {
  72. //Local Declarations
  73. int intNum = 0; //The original number
  74. int lsDigit = 0; //The least significant digit (You will change this value later)
  75.  
  76. //Local Statements
  77.  
  78. return 0;
  79. }//end main
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92. PROBLEM #3: Division & Yet More Modulo
  93.  
  94. Compose a program that uses both division AND modulo to determine the
  95. quotient and remainder of two integer values provided by the user, then print those two values.
  96. Use the skeleton code below to start.
  97.  
  98.  
  99. /** SKELETON CODE **/
  100.  
  101. #include <stdio.h>
  102.  
  103. int main(void)
  104. {
  105. int intNum1 = 0; //First number
  106. int intNum2 = 0; //Second number
  107. int intQuot = 0; //Quotient
  108. int intRmdr = 0; //Remainder
  109.  
  110. printf("Supply two integer values: ");
  111. //Scanf here
  112.  
  113. //CALCULATE QUOTIENT HERE
  114.  
  115. //CALCULATE REMAINDER HERE
  116.  
  117. return 0;
  118. }//end main
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. PROBLEM #4: Shifty Business
  137.  
  138. Compose a program that declares a defined constant value of 9876543201 called ALONGNBR.
  139. Now you will manipulate the value to accomplish shifting the decimal point to the right,
  140. shifting the decimal point to the left, and returning the rightmost values, printing them to output.
  141.  
  142. Use the division operator to shift the defined number to the right 4
  143. places, effectively truncating the "3201" values. Print out this value.
  144.  
  145. Then use the modulo operator to return the 2 rightmost values of the original number.
  146. Print out this value.
  147.  
  148. Lastly, use the multiplication operator to shift the original number two places to the left.
  149. When you print out the last statement your format modifier should use %lld for a
  150. long long integer.
  151.  
  152.  
  153. SAMPLE OUTPUT:
  154.  
  155. Shift 4 places to the left: 987654
  156. Return 2 rightmost numbers: 1
  157. Shift 2 places to the right: 987654320100
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178. PROBLEM #5: Calculate the Hours and Minutes
  179.  
  180. Compose a program that prompts the user for an whole number of minutes and store it to a
  181. variable. Now use division and/or modulo to determine the number of hours and minutes and
  182. output it to screen.
  183. For example, if the user enters in 349 minutes, the output should be 5 hours and 49 minutes.
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201. PROBLEM #6: Tired of Modulo yet?
  202.  
  203. Compose a program that will take a floating point number as input (float or double works, just keep it consistent)
  204. and will round the number to the hundredth place, rounding up. Sample output below.
  205.  
  206. So if the user enters in the value 2.345,
  207. the program will round the number to 2.35 and output it to the screen.
  208. YOU WILL NEED TO USE SHIFTING (i.e. similar to problem #4) TO DO THIS.
  209.  
  210. Hint: The solution provided to me involves moving the decimal point to the left,
  211. performing a conversion (cast) to int, then shifting the number
  212. back to the right and converting it back to a floating point data type.
  213.  
  214.  
  215. SAMPLE OUTPUT:
  216.  
  217. Enter in a floating point value with at least 3 decimal places.
  218. 2.325
  219. The number is now: 2.330000
  220.  
  221.  
  222.  
  223.  
  224.  
  225. NOTE: Printing a value to screen can round the output as its presented, but the actual value in
  226. memory that is being used for math operations is not rounded, which can cause
  227. errors hidden by the rounded output.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement