Question

2. Assume that n has been declared as an integer variable and received a positive value...

2. Assume that n has been declared as an integer variable and received a positive value which is unknown to us, you write statements to sum all values from 1 to n with a step size at 2 and print out the summation. For example, if n is 4, the summation is 4 because 1 + 3 equals 4; if n is 5 the summation is 9 because 1 + 3 + 5 equals 9.

Homework Answers

Answer #1

Dear Student,

Please find the attached code for the required problem definition:

def sum_n(n):
    # initializing the value of sum variable
    sum = 0
    # setting up the for loop with required step size of 2 starting from 1 to n+1
    for i in range(1,n+1,2):
        sum += i      # adding all the values
    print(sum)        # printing the value of the sum variaable


n = int(input())     # taking user input for n
sum_n(n)             # calling the function 

Thank you, please upvote if you liked it!

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
Given an int variable n that has already been declared and initialized to a positive value...
Given an int variable n that has already been declared and initialized to a positive value and another int variable j that has already been declared , use for loop to print a single line consisting of n asterisk. Thus if n contains 5 , five asterisks will be printed. Use no variables other than n and j. ( please use C standard)
How many ways are there to represent a positive integer n as a sum of (a)...
How many ways are there to represent a positive integer n as a sum of (a) k non-negative integers? (b) k positive integers? Note: the order of summation matters. For example, take n = 3, k = 2. Then the possible sums in (a) are 3+0, 2+1, 1+2, 0+3
In C programming language write a function that takes an integer n as input and prints...
In C programming language write a function that takes an integer n as input and prints the following pattern on the screen: 1 (n times) 2 (n-1 times) .n (1 time) For example, if n was 5, the function should print 1 1 1 1 1 2 2 2 2 3 3 3 4 4 5
PYTHON Ask the user for a value N (0 ≤ N < 10) Create a 2-D...
PYTHON Ask the user for a value N (0 ≤ N < 10) Create a 2-D list in an N X N structure with integers 1-(N*N) Print the list created Reverse the list such that (1) each list in the 2D list is reversed and (2) the order of each list in the outer list is reversed (see example) Print the reversed list Example Execution What size 2D list would you like to create? N> 3 The original list is:...
(Python Programming) Write a program that prompts a user for a positive integer and then uses...
(Python Programming) Write a program that prompts a user for a positive integer and then uses a loop to calculate and display the sum of specific fractions as follows: Let's say the user enters 5, then your program will compute: 1/5 + 2/4 + 3/3 + 4/2 + 5/1 which is 8.7.
Write a MASM program that computes the sum of the integers from 1 to N where...
Write a MASM program that computes the sum of the integers from 1 to N where N is a positive integer. Use the equal sign directive to define N. Save the sum in the EAX register. You must use loops. For example, 1 = 1 1 + 2 = 3 1 + 2 + 3 = 6 1 + 2 + 3 + 4 = 10 1 + 2 + 3 + 4 + 5 = 15 Language ( Assembly)...
IN C++ AS SIMPLE AS POSSIBLE ______ Re-write the given function, printSeriesSquareFifth,  to use a while loop...
IN C++ AS SIMPLE AS POSSIBLE ______ Re-write the given function, printSeriesSquareFifth,  to use a while loop (instead of for). • The function takes a single integer n as a parameter • The function prints a series between 1 and that parameter, and also prints its result • The result is calculated by summing the numbers between 1 and n (inclusive). If a number is divisible by 5, its square gets added to the result instead. • The function does not...
Let N be a positive integer random variable with PMF of the form pN(n)=12⋅n⋅2−n,n=1,2,…. Once we...
Let N be a positive integer random variable with PMF of the form pN(n)=12⋅n⋅2−n,n=1,2,…. Once we see the numerical value of N , we then draw a random variable K whose (conditional) PMF is uniform on the set {1,2,…,2n} . 1. Find joint PMF pN,K(n,k) For n=1,2,… and k=1,2,…,2n 2. Find the marginal PMF pK(k) as a function of k . For simplicity, provide the answer only for the case when k is an even number. For k=2,4,6,… 3. Let...
Let N be a positive integer random variable with PMF of the form pN(n)=1/2⋅n⋅2^(−n),n=1,2,…. Once we...
Let N be a positive integer random variable with PMF of the form pN(n)=1/2⋅n⋅2^(−n),n=1,2,…. Once we see the numerical value of N, we then draw a random variable K whose (conditional) PMF is uniform on the set {1,2,…,2n}. Find the marginal PMF pK(k) as a function of k. For simplicity, provide the answer only for the case when k is an even number. (The formula for when k is odd would be slightly different, and you do not need to...
An integer 'n' greater than 1 is prime if its only positive divisor is 1 or...
An integer 'n' greater than 1 is prime if its only positive divisor is 1 or itself. For example, 2, 3, 5, and 7 are prime numbers, but 4, 6, 8, and 9 are not. Write a python program that defines a function isPrime (number) with the following header: def isPrime (number): that checks whether a number is prime or not. Use that function in your main program to count the number of prime numbers that are less than 5000....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT