Question

In: Computer Science

This is for python coding First of all, you need to know what Natural Numbers are....

This is for python coding

First of all, you need to know what Natural Numbers are. They are the positive integers (whole numbers) 1, 2, 3, etc., and zero as well.

For this project, there are TWO PORTIONS:

PORTION 1:

Write a program to find the SUM of the first n natural numbers the user wants to add.

The program should first prompt the user for how many natural numbers are to be summed. The use for loop to do the calculation. It should then print out a total sum.

PORTION 2:

Write a program to find the SUM of squares of the first n natural numbers the user wants to add.

The program should first prompt the user for how many natural numbers are to be summed. The use for loop to do the calculation. You can use math library function math.pow(x,y) to do the square. It should then print out a total sum.

Solutions

Expert Solution

import math
def SumofNaturalNumber(n):
sum = 0;
for i in range(1,n+1):
sum = sum + i
return sum
def SumofSquaresNaturalNumber(n):
sum = 0;
for i in range(1,n+1):
sum = sum + math.pow(i,2)
return sum
n = int(raw_input("Enter how many natural numbers are to be summed: "))
sum = SumofNaturalNumber(n)
print "Sum of NaturalNumber is",sum
sum = SumofSquaresNaturalNumber(n)
print "Sum of Squaresof NaturalNumber is",sum
====================================================================================
See Output


Related Solutions

python coding Suppose a list of positive numbers is given like the following list (remember this...
python coding Suppose a list of positive numbers is given like the following list (remember this is only an example and the list could be any list of positive numbers) exampleList: 15 19 10 11 8 7 3 3 1 We would like to know the “prime visibility” of each index of the list. The “prime visibility” of a given index shows how many numbers in the list with indexes lower than the given index are prime. For instance, in...
What do you know about programming in Python? What is the difference between Python and Java?
What do you know about programming in Python? What is the difference between Python and Java? What does the term Open Source mean? Name four examples of Open Source software. What is the IDEL programming environment? How does IDEL relate to Python? How do you spread a long statement over multiple lines in Python? How do you use the loop-index? How will knowing and understanding Python impact what you do in your profession and/or personal experiences?
Why does the medical assistant need to know the steps for performing diagnostic coding?
Why does the medical assistant need to know the steps for performing diagnostic coding?
Prove That For All Natural Numbers A > 1 And B > 1, If A Divides...
Prove That For All Natural Numbers A > 1 And B > 1, If A Divides B Then A Does Not Divide B+1 (prove by contradiction)
In a game of chance, first 20 natural numbers are listed on a piece of paper...
In a game of chance, first 20 natural numbers are listed on a piece of paper and three numbers are chosen at random. If the numbers form an A.P. you will get Rs. 10000, If the numbers form a GP you will get Rs. 20000, otherwise you have to pay Rs. 8000. What is the mean expected gain and the corresponding risk, if you play the game?
[PYTHON] How do you write a program that first gets a list of floating point numbers...
[PYTHON] How do you write a program that first gets a list of floating point numbers from input. The input begins with an integer indicating the number of numbers that follow. Then input all data values and store them in a list [PYTHON]
Show that among all collections with 2n-1 natural numbers in them there are exactly n numbers...
Show that among all collections with 2n-1 natural numbers in them there are exactly n numbers whose sum is divisible by n.
Task 3      Write a program that pushes the first 10 natural numbers to the stack,...
Task 3      Write a program that pushes the first 10 natural numbers to the stack, then pops those numbers. Use the debugger to view how the stack changes after each instruction. Once all the data has been pushed to the stack, take a screenshot of the stack in memory. Task 4     Write a subroutine called “Area” that calculates the area of a rectangle. Use accumulator A and B to pass the length and the width to the function....
**To Be Written in Python for Beginners** Overview Do you know what an alternade is? Of...
**To Be Written in Python for Beginners** Overview Do you know what an alternade is? Of course you don’t, no one does! According to Wikipedia, an alternade: “is a word in which its letters, taken alternatively in a strict sequence, and used in the same order as the original word, make up at least two other words. All letters must be used, but the smaller words are not necessarily of the same length. For example, a word with seven letters...
The first random number generator comes from Python itself. To use it you need to import...
The first random number generator comes from Python itself. To use it you need to import the Python package. Then call the random() method. See below. import random print (random.random()) It is important to know that a random number generator really is random. If so, it should have uniform distribution between 0 and 1. Test the random number generators in Python for uniformity.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT