M4ritimeSeeker

PASS Week 5: Conditions, If-Else, and Logic

Feb 14th, 2022 (edited)
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. PROBLEM #1: If-Else Statements
  2.  
  3. Create a program that will prompt the user to enter in two integer
  4. values; Store them in the variables nbr1 and nbr2 (These steps have been done for you).
  5. Use an if/else construct to print out the following conditions if they
  6. are true “nbr1 <= nbr2” or “nbr1 > nbr2”.
  7.  
  8. SAMPLE OUTPUT:
  9. Please enter two integers: 4 5
  10. 4 <= 5
  11.  
  12. MORE SAMPLE OUTPUT:
  13. Please enter two integers: 7 7
  14. 7 <= 7
  15.  
  16.  
  17.  
  18. /****** SKELETON CODE ******/
  19.  
  20. #include <stdio.h>
  21.  
  22. int main (void)
  23. {
  24. // Local Declarations
  25. int nbr1;
  26. int nbr2;
  27.  
  28. // Statements
  29. printf("Please enter two integers: ");
  30. scanf ("%d%d", &nbr1, &nbr2);
  31.  
  32. //YOUR CODE HERE
  33.  
  34. return 0;
  35. } // end main
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52. PROBLEM #2: Nesting If-Else Statements
  53.  
  54. In this problem, you will go deeper with your If-Else statements by nesting (inserting) ANOTHER If-Else
  55. statement inside of an already existing one.
  56. Modify the program from PROBLEM #1. INSIDE the block that checks nbr1 <= nbr2, insert
  57. another if-else block.
  58. Then, use the new if-else block to determine which case is true: "nbr1 < nbr2" or "nbr1 == nbr2"
  59. And print the appropriate message to the user for each condition.
  60.  
  61. SAMPLE OUTPUT:
  62. Please enter two integers: 5 6
  63. 5 < 6
  64.  
  65. MORE SAMPLE OUTPUT:
  66. Please enter two integers: 7 7
  67. 7 == 7
  68.  
  69.  
  70.  
  71.  
  72. /****** SKELETION CODE ******/
  73. USE YOUR SOLUTION FOR PROBLEM #1
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95. PROBLEM #3: Calculating a student's Grade
  96.  
  97. In this program you will be calculating a student's Letter Grade
  98. for an assignment. The variables for the score and the letter grade
  99. have been made for you, as well as the printf and scanf for input.
  100.  
  101. Using the skeleton code below, ADD a switch statement in the
  102. program that takes the score variable and prints the char variable that
  103. corresponds to the letter grade earned. 90 to 100 is an ‘A’, 80 to 89
  104. is a ‘B’, etc.
  105. HINT: You can narrow the number of cases by changing the value from a 93 to a 9 or a 26 to a 2 by using integer division.
  106.  
  107. Use this link to help out
  108. https://www.tutorialspoint.com/cprogramming/switch_statement_in_c.htm
  109.  
  110.  
  111.  
  112.  
  113. /****** SKELETON CODE ******/
  114.  
  115. #include <stdio.h>
  116.  
  117. int main (void)
  118. {
  119. // Local Declarations
  120. double score = 0.0;
  121. char grade;
  122.  
  123. // Local Statements
  124. printf("Enter the test score (0-100): ");
  125. scanf ("%lf", &score);
  126.  
  127.  
  128. //YOUR SWITCH STATEMENT HERE
  129.  
  130.  
  131. printf("The grade is: %c\n", grade);
  132.  
  133. return 0;
  134. } // main
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149. PROBLEM #4: Creating and Using Flags
  150.  
  151. Often a program will need to change its execution based on some user condition or exception,
  152. like if the user requests to QUIT the program or there is an ERROR detected.
  153. As a programmer, here is what you can do to help with this: Flags.
  154. A flag variable can be set to a condition and a switch statement can be a handy way to determine
  155. what to do given that condition.
  156.  
  157. Write a program that prompts the user to enter in an integer value 1 thru 3.
  158. In the case that the value is 1 or 2, print “An error occurred.\n”.
  159. In the case that the value is 3 print out “google i lost superbowl bet and am 8 million in debt what do i do.\n”.
  160. USE SWITCH-CASE TO CHECK. NOT IF-ELSE.
  161. After checking the cases, regardless of value of the flag, print out a message to the user
  162. letting them know the program is terminating with the message
  163. “Good luck bro, goodbye.\n”.
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183. PROBLEM #5: Find the median value of three values
  184.  
  185. Using nested if/else conditions, write a program that will prompt for then take three
  186. unordered integer values as input. Your if/else construct should determine which value is
  187. the median valueof the three. If you use a comparison operator ‘==’ make sure not to
  188. confuse it with the ‘=’ assignment operator. It might make it easier to draw the various
  189. conditions using pencil and paper. This is an example of where a flowchart design tool could
  190. help verify your logic.
  191.  
  192.  
  193.  
  194. /****** SKELETON CODE ******/
  195.  
  196. #include <stdio.h>
  197.  
  198. int main(void)
  199. {
  200. int a, b, c, median;
  201. printf("Enter in 3 integer values.\n");
  202. scanf("%d", &a);
  203. scanf("%d", &b);
  204. scanf("%d", &c);
  205.  
  206. //Your code here
  207.  
  208.  
  209. return 0;
  210. }
Advertisement
Add Comment
Please, Sign In to add comment