Here, we are going to write python program to find sum of squares of n natural numbers. To find sum of squares of n numbers we can use for loop or while loop to add the squares of each numbers one by one into sum.
Sum of squares of n natural numbers is the sum of squares of the positive numbers from 1 to nth number.Lets see practical solved example. We can use following formula to solve the query.
Formula:
sum of squares of n numbers:
12+22+32+42+52+….+(n-1)2+n2
Examples
sum of squares of 3 numbers:
12+22+32 = 1 + 4 +9 = 14
Program 1 : Python program to find sum of squares of n numbers using for loop
# Program to find sum of sqaure of n numbers
# reading input from user
n = int(input("Enter any number : "))
# initialising sum to zero
sum = 0
# use for loop to iterate through given number
for i in range(1, n+1):
sum = sum + (i*i)
# Printing sum of squares
print("Sum of squares is : ", sum)
Output:
Run 1 :
Enter any number : 100
Sum of squares is : 338350
%Run 2:
Enter any number : 10
Sum of squares is : 385
%Run 3:
Enter any number : 3
Sum of squares is : 14
Program 2 : Python program to find sum of squares of n numbers using while loop
# Program to find sum of sqaure of n numbers
# reading input from user
n = int(input("Enter nth number : "))
sum = 0
# use while loop
while n>0:
sum = sum + (n*n)
n = n-1
# Printing sum of squares
print("sum of squares is : ",sum)
Output:
Run1
Enter nth number : 200
sum of squares is : 2686700
Run2
Enter nth number : 150
sum of squares is : 1136275%Run 3
Enter nth number : 5
sum of squares is : 55
Other python programs
- Write a program to create Pandas series from dictionary of values and nd array.
- Write a program to perform mathematical operation on two Pandas series object.
- Write a program to create data frame quarterly sales where each row contain the item category, item name and expenditure. Group the row by the category and print the total expenditure per category.
- Write a program to create data frame based on e-commerce data and generate descriptive statistics.
- Write a program to create data frame for examination result and display row labels, column labels data types of each column and the dimensions.