Question

4. Show your output that only even numbers from 80 to 115 get printed. Hint. You...

4. Show your output that only even numbers from 80 to 115 get printed. Hint. You can use modulo (%%) in Chapter 1. Even numbers have the remainder 0 after division of the number by 2. You start with the following code for a) and b): number <- 80

a) Use repeat loop

b) Use while loop You start with the following code for c): number <- seq(from = 80, to = 115)

c) [ Use for loop

Homework Answers

Answer #1

a) Use repeat loop

number <- 80
repeat {
if (number %% 2 == 0){
print(number)
}
number = number + 1
if (number == 115){
break
}
}

b) Use while loop

#R version 3.3.2
  
number <- 80
while (number < 115) {
if(number %% 2 ==0)
print(number)
number = number+1
}

c) Use for loop

#R version 3.3.2
  
number <- seq(from = 80, to = 115)
for (val in number) {
if(val %% 2 == 0)
print(val)
}

OUTPUT:

 80
 82
 84
 86
 88
 90
 92
 94
 96
 98
 100
 102
 104
 106
 108
 110
 112
 114
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
2. Using a “for loop” print all even numbers in rage from 1 to 1000. Also,...
2. Using a “for loop” print all even numbers in rage from 1 to 1000. Also, please count and print how many even numbers you have found. 3. Using the following list and a “for” loop, display differences of all consecutive pairs of numbers in the list. our_list = [1,2,5,6,3,77,9,0,3,23,0.4,-12.4,-3.12] The output should look like this: 1 3 1 … 4. Using a “while loop” ask the user for a number and add it to a list if number is...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: a) Complete the int Q7a(intQ7_input) function takes a seven-digit positive integer as input and returns it reversed. For example, if the integer is 9806593, the program should print 3956089. You are not permitted to use any function of C standard library other than scanf()and printf().You are not permitted to use...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: intQ1_for() intQ1_while() intQ1_do() To compute the sum of all numbers that are multiples of 4, between 30 and 1000, in 3 different ways: with a for loop, a while loop and a do-while loop, accordingly. After each loop print the value. Return the total sum at the end of each...
***Python Hailstones, also known as the Collatz sequence, are a mathematical curiosity. For any number in...
***Python Hailstones, also known as the Collatz sequence, are a mathematical curiosity. For any number in the sequence, the next number in the sequence is determined by two simple rules: If the current number n is odd, the next number in the sequence is equal to 3 * n + 1 If the current number n is even instead, the next number in the sequence is equal to one half of n (i.e., n divided by 2) We repeat this...
Uses a while loop to print the numbers from 3 - 19. Uses a do-while loop...
Uses a while loop to print the numbers from 3 - 19. Uses a do-while loop to print the numbers from 42 - 56. Uses a for loop to print the numbers from 87 - 95. Asks the user for 2 numbers. Uses a loop to print all numbers between the given numbers, inclusive. Note: Consider that your user's second number can be lower! (see example below) Note: Also consider that your user might give you two of the same...
Practice Activity -- Working with Counting Loops -- Part 2 Objective: to practice writing programs that...
Practice Activity -- Working with Counting Loops -- Part 2 Objective: to practice writing programs that use counting loops Instructions: This is the second part of a 2-part practice activity. Be sure to complete Part 1 before doing these exercises. Write a Python program to solve each of the following in the order shown (in each pattern, the intention is that each number be printed on a separate line; no commas are required): Use a 'while' loop to create this...
If you roll a die, you get one of the following numbers: 1, 2, 3, 4,...
If you roll a die, you get one of the following numbers: 1, 2, 3, 4, 5, 6. Each possibility occurs with equal probability of 1/6. The expected value of a dice roll is E(D)= 3.5 and the variance of a dice roll is Var(X) = 2.917. a) Suppose you roll a die and then add 1 to the roll to get a new random variable taking one of the following numbers: 2,3,4,5,6,7. What is the variance of this new...
C++ Goals:Practicing arrays Create a program that will read whole numbers from a file called Labs4-7Mu.dat...
C++ Goals:Practicing arrays Create a program that will read whole numbers from a file called Labs4-7Mu.dat (posted on Canvas)and store it into an array. The number of values in the file is less than 300 and all the values are whole numbers. The actual number of values stored in the file should be determined. Your program should then prompt the user to enter another whole number between 2 and 20 (you may assume the user enters a valid value) and...
In C# using the Console App (.NET FRAMEWORK) Create and test a Windows Console application that...
In C# using the Console App (.NET FRAMEWORK) Create and test a Windows Console application that displays the following patterns separately, one after the other. You MUST use nested for loops to generate the patterns, like the example in the PowerPoint slides from this chapter. All asterisks (*) should be displayed by a single statement of the form Console.Write("*"); which causes the asterisks to display side by side. A statement of the form Console.WriteLine(); can be used to move to...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: A positive integer number is said to be a perfect number if its positive factors, including 1 (but not the number itself), sum to the number. For example, 6 is a perfect number because 6=1+2+3. Complete the int Q6(intQ6_input, int perfect[])function that determines all perfect numbers smaller than or equal...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT