Exercise 11: for Loops
Explore how to write for loops in R to iterate through vectors and print specific messages for numbers that are multiples of 3, 5, or both. Understand how to use the cat() function for clear output and practice solving problems that apply conditional logic inside loops.
We'll cover the following...
We'll cover the following...
Problem Statement
For each element in the given vector, print foo if it is a multiple of , bar if it is a multiple of and foo bar if it’s a multiple of both and .
Use the
cat()function for printing. Remember to print the number as well as the respective statement!
Input
A testVariable containing the vector of numbers
Output
The element and the attached statement with it.
Sample Input
testVariable -> c(3, 5, 15)
Sample Output
3 foo 5 bar 15 foo bar
Test Yourself
Write your code in the given area. If you get stuck, you can look at the solution.