exercises

# exercises - 2 CONDITIONALS Answers to select problems are...

This preview shows pages 1–4. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 2 CONDITIONALS Answers to select problems are at the end. 1 Statements and printf() 1. printf() has a very large number of format specifiers. There are a number of options that can be used with the format specifiers for altering the way that information is displayed. Run the following program with different values for the format specifiers (sorry about using loops before discussed in class, but it makes this much simpler). #include <stdio.h> int main(void) { int i; double x; for(i = 1; i < 10000; i *= 10) printf("%d\n", i); for(x = 1.234; x < 10000; x *= 10) printf("%lf\n", x); } For the first printf() , some values to try are %2d and %4d . For the second printf() , some values to try are %2lf , %4.2lf , and %8.5lf . 2. Write a program that prompts a user for an integer and then evaluates the following mathematical function for that integer: f(x) = 10 - 18x + 6x^2 2 Conditionals 1. Compare the output from the following programs. Why is it different? #include <stdio.h> int main(void) { int x = 8; if(x < 6) printf("%d < 6\n", x); else if(x < 8) printf("%d < 8\n", x); else if(x < 10) 1 3 LOOPS printf("%d < 10\n", x); else if(x < 12) printf("%d < 12\n", x); else if(x < 14) printf("%d < 14\n", x); } and #include <stdio.h> int main(void) { int x = 8; if(x < 6) printf("%d < 6\n", x); if(x < 8) printf("%d < 8\n", x); if(x < 10) printf("%d < 10\n", x); if(x < 12) printf("%d < 12\n", x); if(x < 14) printf("%d < 14\n", x); } 2. The following program doesn’t print anything. Why? Using only a set of curly braces, change the program such that the second printf() statement will print. #include <stdio.h> int main(void) { int age = 40; if(age > 50) if(age < 75) printf("first printf\n"); else printf("second printf\n"); } 3 Loops 1. Rewrite the following program, replacing the for loop with a while loop. 2 4 FUNCTIONS #include <stdio.h> int main(void) { int i; for(i = 8; i > 5; i--) printf("%d\n", i); } 2. Rewrite the following program, replacing the do-while loop with a for loop. This may require more than just changing the loop. #include <stdio.h> int main(void) { int x = 5; do { printf("%d\n", x); x++; } while(x < 5); } 3. Write the code to sum the integers 7 through 11 inclusive (i.e., include 7 and 11). Use a loop to do it. 4. You have a program that prompts a user for a positive integer, which is stored in the variable stop . Write the code that will sum the integers from 1 to stop and then print out the answer. For example, if the user entered 3, the answer printed out would be 6 since 1 + 2 + 3 = 6. 5. You have a program that prompts a user for two integers, which are stored in the variables start and stop . Write the code that will go from start to stop , printing “x is Even” if the number is an even number and “x is Odd” if the number is odd, replacing x with the integer value....
View Full Document

{[ snackBarMessage ]}

### Page1 / 37

exercises - 2 CONDITIONALS Answers to select problems are...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online