Question

Using a for loop and range command, print the sequence of numbers from 1 to 20...

Using a for loop and range command, print the sequence of numbers from 1 to 20 (skipping two numbers for each element); that is, your code should print out the numbers 1,4, 7, 10, … (max should not exceed 20). Use x as the variable name

Homework Answers

Answer #1

In python programming language, we can print sequence of numbers using for loop and range command.

Given data,

Input: Numbers from 1 to 20

Output: sequence of numbers 1,4,7,10....

Condition: output should not exceed 20 and skipping two numbers for each element.

Program:

# consider variable name as x

for x in range(1,20+1,3):

print(x)

Explanation:

we have to give 20+1 because range considers the given number with -1. It means if we have given as 20 it considers the value upto 19 only. So inorder to print upto 20 give as 20+1.

The syntax for range is :

range(start value, stop value, increment steps)

In our code, range starts from 1 and ends with 20 with increment of 3 steps.

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
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...
In Java programming language 11.Create the code for a for loop to print the numbers 1  through...
In Java programming language 11.Create the code for a for loop to print the numbers 1  through 5 inclusive vertically so the output is 1 2 3 4 5 11b What is the output of the following               OUTPUT int i=3; while(int i >0) { System.out.println(i); i--; } 11c What is the output of the following               OUTPUT for(int i=3;i<10;i++) System.out.println(3*i); 11d Create the line of code to print the numbers 10 through 1 decreasing by 1 each time 11e Create the line of code...
1) Write a java programming using a while loop where you will add numbers and when...
1) Write a java programming using a while loop where you will add numbers and when you press number 0 it will add all your numbers and display the results. Use Scanner object. Steps: 1) Declare variables integer called sum which is equal to zero   2) Declare Scanner object   3) Declare while condition where is true   4) Inside of that condition prompt the user to enter the numbers   5) Declare variable integer called number and input the number statement   6)...
Using a for Loop visual c++ Summary In this lab the completed program should print the...
Using a for Loop visual c++ Summary In this lab the completed program should print the numbers 0 through 10, along with their values multiplied by 2 and by 10. You should accomplish this using a for loop instead of a counter-controlled while loop. Instructions Write a for loop that uses the loop control variable to take on the values 0 through 10. In the body of the loop, multiply the value of the loop control variable by 2 and...
USING MATLAB (A) Use the rand command to generate “X” of size [10000 1] and the...
USING MATLAB (A) Use the rand command to generate “X” of size [10000 1] and the command histogram to plot the probability distribution of “X” along with axis labels and a title that includes the mean and variance for “X”. (B) Use a while loop and the rand command to generate “X1”, also of size [10000 1] but with each element of “X1” being the mean of 2 “samples” generated using rand, i.e. a “sample size” of 2 separate numbers...
Write a code segment to print your name ten times on the screen using a while...
Write a code segment to print your name ten times on the screen using a while loop. Use one variable called count to act as a counter for the loop. Discuss how would you print your name 1000 times on the screen. NOTE: You can either type your code into the text box or upload a document to the discussion board by clicking the "attach" clip at the bottom left of the discussion box.
Take 10 numbers from the user. Calculate their mean and median with your code, not using...
Take 10 numbers from the user. Calculate their mean and median with your code, not using builtin Matlab functions. Print these values on the screen with a special format using the command: fprintf.
***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...
1. Write a macro in SAS: It should be able to use proc means to print...
1. Write a macro in SAS: It should be able to use proc means to print means of a quantitative variable BY a categorical variable (must use a BY line of code), and use proc sgplot to print boxplots of the same quantitative variable by the categorical. Your macro should sort the data first. The user should be able to call the macro, indicating the data set name, and the two variables used in the analysis, as parameters. (You’ll use...
FOR PROBLEMS 1 AND 2. Use the sequence formulas. Carry out your calculations to the final...
FOR PROBLEMS 1 AND 2. Use the sequence formulas. Carry out your calculations to the final number. Round the results to 1 decimal place.    1. Consider the sequence: 1/2, 1, 2, 4, 8, a. Find the 30th term of the sequence. b. Find the sum of the first 30 terms of the sequence. 2. You give your daughter a piggy bank with $2 in it. She puts $5 in it the next week and each week thereafter. How much...