Uploaded by mnomanabbbasi10

practice question of programming fundamentals(for loop,if or else)

advertisement
CS1001aL
Programming in C++
LAB 5: Functions & Loops
Objectives:
In this Lab, you will learn:
 Learn about Functions
 Learn how to use for loop.
Lab Tasks
1. Create a program that reads three integers from the user and displays the maximum and
minimum integer. Create functions min and max functions to find the smallest and largest
values.
int max(int num1, int num2, int num3)
int min(int num1, int num2, int num3)
2. The distance a vehicle travels can be calculated as follows:
distance = speed * time
For example, if a train travels 40 miles per hour for 3 hours, the distance traveled is 120 miles.
Write a function distanceTraveled that receives the speed of a vehicle and hours it has
traveled. The program should then use a loop to display the distance the vehicle has traveled
for each hour of that time period. Here is an example of the output:
What is the speed of the vehicle in mph? 40
How many hours has it traveled? 3
Hour Distance Traveled
-------------------------------1 40
2 80
3 120
void distanceTraveled ( int speed, int hours)
3. Write a function isEven that takes an integer as argument and displays a message
indicating whether the integer is even or odd. Function will return nothing.
void isEven ( int number)
4. A prime number is a number that is only evenly divisible by itself and 1. For example, the
number 5 is prime because it can only be evenly divided by 1 and 5. The number 6,
however, is not prime because it can be divided evenly by 1, 2, 3, and 6.
Write a function name isPrime, which takes an integer as an argument and returns 1 if the
argument is a prime number, or 0 otherwise.
int isPrime ( int number)
CASE Institute
Page 1 of 1
Download