Basic arithmetic, input/output, and conditionals and loops


Exercise 0

Read the C Programming textbook (or other online C reference) for input/output using the following standard C functions:

You should also use the man command to read the online manual pages for each of these functions. Make sure you understand the format strings used by scanf() and printf() to specify the format of the input/output, especially reading/writing characters, strings, integers, and floats. Make sure you understand how the width (i.e., number of characters used) is specified in a format string, and also how left- vs. right-justification is specified.


Note

For the exercises below, create a folder lab3 inside the comp411lab folder that you already have (from Lab 2) under your home directory. For the four exercises below, you will code your C programs in the files ex1.c, ex2.c, ex3.c and ex4.c, respectively.


Exercise 1

Write a program that does the following: It prompts the user with "Please enter a number from 1 to 5: " (note the space after the colon), reads the user's response, and then prints "Hello World" (with a newline at the end) as many times as indicated by the user's response, and terminates. That is, if the user enters 3, your program will print "Hello World" three times on three different lines. If the user enters a number that is outside of the 1..5 range, the program should print the error message "Number is not in the range from 1 to 5" (with a newline at the end), and terminate.

The file with your C program should be called ex1.c. It does not matter what you name the executable (e.g., a.out, ex1, etc.). Compile the C program using the C compiler (cc or gcc), and test it with a few different inputs. You can find sample test input and output files as described in the accompanying lab writeup.


Exercise 2

Write a program that requests three single-precision floating-point numbers (floats), and prints their sum and product to four decimal places. Specifically, the program should prompt the user with this message "Enter three floating-point numbers: " (note the space after the colon). It should print the sum and product formatted exactly as follows:

Sum is -123.8765
Product is 0.0019

Name the file with the C program ex2.c. You can find sample test input and output files as described in the accompanying lab writeup.


Exercise 3

Write a program that requests six integers ("Enter six integers: "), reads all of them, then prints all of them in the following format: (i) first print a header line as shown in the example below; then (ii) two integers per line, with each integer right-justified in a field of 10 characters, separated by two blank spaces. For example, if the numbers input are 1, 10, 20, 25, 1000, -200, then the output should be exactly as follows:

1234567890bb1234567890
         1          10
        20          25
      1000        -200

Name the file with the C program ex3.c. Test it on the sample input and output files provided.


Exercise 4

Write a program that repeatedly reads an integer, and determines if it is prime or non-prime. Specifically, the program should do the following:

Note that the C language provides / and % operators for integer divide and modulus operations.

For example, the following is one execution scenario:

Number [1-100]: ? 5
Prime
Number [1-100]: ? 1
Prime
Number [1-100]: ? 2
Prime
Number [1-100]: ? 27
Non-prime, divisible by 3
Number [1-100]: ? 0
Done

First test your program by running it through the execution scenario above, and make sure it produces exactly the same output. Name the file with the C program ex4.c. Test it on the sample input and output files provided.


Written: 7 September 2012, Montek Singh, montek@cs.unc.edu