C++ Functions
Examples (3/6/13):
Answer and discuss the following questions:
Complete these programming projects:
1. Write and test the void
function printfactorial( int n )
that prints the factorial of an input integer. For example:
If printfactorial( 3 ), output is: 3! = 3 * 2 * 1 = 6 If printfactorial( 4 ), output is: 4! = 4 * 3 * 2 * 1 = 24
See printfactorial program developed in class.
2. Write a C++ program printTable.cpp
that utilizes looping and the tab escape sequence \t
to print the following table of values:
N 10*N 100*N 1000*N 1 10 100 1000 2 20 200 2000 3 30 300 3000 4 40 400 4000 5 50 500 5000
3. Write a program evenFunction.cpp
that inputs a series of integers and passes them one at a time to a function called even
, which uses the modulus (%) operator to determine whether the integers are even. The even
function should take an integer input parameter and have an integer return
type. The program should print whether each integer that you input is EVEN
or ODD
.
4. In the program temperatureTable.cpp
implement the following functions:
- Function
toCelsius
returns the Celsius equivalent of a Fahrenheit temperature. - Function
toFahrenheit
returns the Fahrenheit equivalent of a Celsius temperature.
All inputs to and returns from the functions will be integer. Use these two functions in the temperatureTable.cpp
program to print charts showing the Fahrenheit equivalents of all Celsius temperatures from 0 to 100 degrees, and the Celsius equivalents of all Fahrenheit temperatures from 32 to 212 degrees. Print the outputs in a neat tabular format that minimizes the number of lines of output while remaining readable.
Note:
celsius = 5.0 / 9.0 * ( fahrenheit - 32 ); fahrenheit = (9.0 / 5.0 * celsius) + 32 ;