



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




For this lab, you will use:
All programming languages provide constructs to execute a section of code conditionally. In this week’s lab, you will implement decision making with the use of one or more of the following conditional statements.
if Statements The simplest conditional statement is the if statement. It is used when we want the computer to maybe execute some code based on the truth value of some condition. If the condition is true, the code in the body of the if statement will execute; if it is false, the body of the if statement will be skipped and execution will continue with the code immediately after the if statement. Structure: if (condition) { one-or-more statements; } Example of an if statement: if (n < 2 ) { printf(“Hello\n”); } The above code prints a message only if the value of n is less than two. Otherwise, it does nothing. What does the following code print if the value of n is 1? if (n < 2 ) printf(“Hello ”); printf(“Tigers!!\n”); What does it print if the value of n is 2?
if-else Statements The previous if statement included code that executes when a condition is true. If we want certain code to execute when a condition is true and other code that executes when a condition is false, we use if-else statements. Exactly one of the two possible branches will be taken with an if-else statement. Structure: if (condition) { one-or-more statements; } else { one-or-more statements; } Example: if ( (age >= 13) && (age <= 19) ) { printf(“You are a teenager.\n”); } else { printf(“You are not a teenager.\n”); }
if-else-if Statements When there are more than two options, instead of nesting if-else statements, C provides us with the if-else-if construct. Structure: if (condition) { one-or-more statements; } else if (condition) { one-or-more statements; } else if (condition) { one-or-more statements; } else { one-or-more statements; } Note: The last else is optional and works well whenever there is a fall through case when all other conditions are false. If there is no fall through option needed, then it may make sense to not have that last else at all.
Part I: Write a program called lab6.c which will prompt the user to enter a three digit number on the keyboard. Your program will first find the square root of the number using the sqrt() function from the math library, and print the result to the user. Then, your program will determine if all 3 digits are even numbers, or all 3 are odd numbers, or if they are a mix of even and odd. For example, if the user enters 200, your program will determine that all 3 digits are even numbers. If the user enters 463, then your program will determine that the digits are made up of a mixture of even and odd numbers. Your output should look similar to the following: Enter a three-digit integer: 321 The square root of 321 is 17.92. This number contains a mix of even and odd digits. Part II: Once you are certain that your program works with all three cases (all even, all odd, or a mix), add code to make the program loop continually, asking the user if they want to go again, using a 1 for yes and a 0 for no. If they enter a 1, then they will be prompted to enter a three digit number on the keyboard again. If they enter a 0, then the program will quit. After you add this code, your output should look similar to the following: Enter a three-digit integer: 703 The square root of 703 is 26.51. This number contains a mix of even and odd digits Do you want to go again? (1 for yes, 0 for no): 1 Enter a three-digit integer: 200 The square root of 200 is 14.14. This number contains all even digits Do you want to go again? (1 for yes, 0 for no): 1 Enter a three-digit integer: 333 The square root of 333 is 18.25. This number contains all odd digits Do you want to go again? (1 for yes, 0 for no): 0 Once you are certain that your program works with the addition of the loop, add some code that will check for invalid input for going again. For example: Enter a three-digit integer: 432 The square root of 432 is 20.78. This number contains a mix of even and odd digits. Do you want to go again? (1 for yes, 0 for no): 3 Invalid input. Enter a 1 for yes, 0 for no: 3 Invalid input. Enter a 1 for yes, 0 for no: 5 Invalid input. Enter a 1 for yes, 0 for no: 1 Enter a three-digit integer: 222 The square root of 222 is 14.90. This number contains all even digits. Do you want to go again? (1 for yes, 0 for no): 0 Only add a check to see if they entered a number other than 1 or 0 to go again – you don’t need to check for other characters.
Part III: Create a file called input.txt and in that file, put the inputs to the program that would otherwise have been entered from the keyboard. Using the first example output from Part II above, your input file would contain the following: Run your program again, redirecting the input, like the following (you should see the same results as above): ./a.out < input.txt Then try the following: ./a.out < input.txt > output.txt Type ls to verify that the file output.txt was created. Then type the following to view the contents of that file: cat output.txt
Show your ta that you completed the assignment. Then submit your lab6.c file to the Vocareum web page.
For this lab, points will be based on the following: Functionality 80 (correct use of conditionals: 60; loop to go again: 20) Formatting 10 Use of sqrt() function 5 Catches invalid input 5 (any number not a 1 or 0; don’t worry about catching other invalid input, such as a character)
Reminder About Formatting and Comments