Question

In: Computer Science

1.write a small program using a loop to add a series of numbers 2.write a function...

1.write a small program using a loop to add a series of numbers

2.write a function called "main" that performs several given steps. Be sure to call the main() function so that its code executes

In python and doesn't have to be long. just long enough to do what it says. Thank you.

Solutions

Expert Solution

1) the code to add series of numbers is :

for num in range(0, number+1):
    sum += num

considering the "number" is already given and sum is also assigned to zero

2) the code to make function called "main()" and perform the above operation is:

def main():
  number = int(input("Enter any number: "))
  sum = 0 # it will hold the sum 
  # for is use to loop from zero to given number
  for num in range(0, number+1):
    sum += num
  print("The sum from zero to", number, "is", sum, end="")

The above fucntion will take input from the user and add a series of number and print them

Please refer to the screenshot of the code to understand the indentation of the code.

Output:

Hope this helps you. If you have any doubt ask in the comments section. Thank you


Related Solutions

Q1:Write a Java program to find Fibonacci Series using 1) using for loop 2) using while...
Q1:Write a Java program to find Fibonacci Series using 1) using for loop 2) using while loop To Understand what the Fibonacci series is: The Fibonacci sequence is a series of numbers where a number is the sum of the previous two numbers. Starting with 0 and 1, the sequence goes 0, 1, 1, 2, 3, 5, 8, 13, 21, and so on. Q2: Write a Java program to find the Factorial of a number using a while loop. To...
Write a complete program to sum the following series: (hint: use the for loop) 1 /2...
Write a complete program to sum the following series: (hint: use the for loop) 1 /2 + 1/ 4 + 1 /6 + ⋯ + 1 /100 PS: use C++ language please
write a program bus management system? using: if else, for loop, do while loop, function, arrays,...
write a program bus management system? using: if else, for loop, do while loop, function, arrays, string, structure
Write a MASM program that uses a loop to multiply 2 numbers. Use the equal sign...
Write a MASM program that uses a loop to multiply 2 numbers. Use the equal sign directive to define the two numbers. Save the product in the EAX register. Hint 4 x 5 = 4 + 4 +4 + 4 + 4 = 5 + 5 + 5 + 5 Language (Assembly) ASAP
Using visual studio in MASM x86, Write a program to add the following word size numbers:...
Using visual studio in MASM x86, Write a program to add the following word size numbers: 15F2, 9E89, 8342, 99FF, 7130 using adc instruction and a loop. The result must be in DX, AX. Show the result in debug window.
​​​​​​​For java program. Write a while loop that will let the user enter a series of...
​​​​​​​For java program. Write a while loop that will let the user enter a series of integer values and compute the total values and number of values entered. An odd number will stop the loop. Display the number of iterations and the total of the values after the loop terminates. for Loop Write a for loop to display all numbers from 13 - 93 inclusive, ending in 3. Write a for loop to display a string entered by the user...
Write a program using c++. Write a program that uses a loop to keep asking the...
Write a program using c++. Write a program that uses a loop to keep asking the user for a sentence, and for each sentence tells the user if it is a palindrome or not. The program should keep looping until the user types in END. After that, the program should display a count of how many sentences were typed in and how many palindromes were found. It should then quit. Your program must have (and use) at least four VALUE...
#Python program using loop instead of numpy or pandas. Write a function to enter n student...
#Python program using loop instead of numpy or pandas. Write a function to enter n student scores from 0 to 100 to represent student score, Count how many A (100 - 90), B(89-80), C(79-70), D(69-60), F(<60) We will use numpy array OR pandas later for this problem, for now use loop Invoke the function with 10 student scores, show the result as follow: Test Enter 10 scores Enter a student score: 99 Enter a student score: 88 Enter a student...
Write a Python program to add, multiply and divide any two numbers.
Write a Python program to add, multiply and divide any two numbers.
In the Main method add the code that counts by 1, 2, 3, 4, and 5 – see output below Use a nested for loop to implement the series of numbers C# Syntax:
code in C# How to count by n:   In the Main method add the code that counts by 1, 2, 3, 4, and 5 – see output below Use a nested for loop to implement the series of numbers C# Syntax:  The C# syntax for variable declarations, and if, if-else, while, and for statements is identical to Java   C# console output:  Console.Write("{0,2} ", number); // in this context number is an integer variable writes a number right-aligned in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT