Lab 3 Basic Arithmetic, Input/Output, and Control

| categories: Labs

Due 23 September before the start of lab.

Exercise 1

Read about the following standard C functions for input/output either in the C reference manual recommended or on the web. You'll find lots of useful info by Googling for C language tutorial.

  • getchar()
  • putchar()
  • gets()
  • puts()
  • scanf()
  • printf()

You should also use the man command to skim the online manual pages for each of these functions.

Write a program that does the following: prompt the user to enter a number from 1 to 5, read the response, and print "Hello World" (with a newline at the end) as many times as indicated by the user's response. That is, if the user enters 3, then your program will print "Hello World" three times on three different lines.

Run this program with three different inputs, and show the output obtained.


Exercise 2

Write a program that requests three single-precision floating point numbers (float), and prints their sum and product to four decimal places.

Show the output of your program by running it on two different sets of input.


Exercise 3

Write a program that requests 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

Show the output of your program by running it on two different sets of input.


Exercise 4

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

  • Prompt the user to enter a number with "Number [1-100]: ? "
  • Verify the user entered a number within that range, or 0 to stop the program.
  • If the number entered is a prime number, print "Prime", otherwise print "Non-prime, divisible by xx", where "xx" is the smallest divisor of that number. In either case, the output should be terminated by a newline.
  • Repeat, prompting for the next input, until the user enters a 0, in which case the program prints "Done", followed by a newline, and terminates.

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. Next, run your program with an input of your choice (5 different numbers before ending with a 0), and show the output.


What to submit

Submit your report electronically on Blackboard as a plain text file or as a pdf. Your report should include the input and output of each program and the program code.


Written: 26 January 2011, Montek Singh, montek@cs.unc.edu, Modified: 14 September 2011 by Gary Bishop.