In: Computer Science
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.
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