Saturday, 21 September 2024

Practical - 73 - Write a Python program to print the sum of first n odd natural numbers where the value of n is taken as input from the user.

Practical - 73 - Write a Python program to print the sum of first n odd natural numbers where the value of n is taken as input from the user.


Ans:

n = int(input("Enter the value of n: "))

sum=0

for i in range(1, 2*n, 2):

    sum+=i

print("The sum of the first",n,"odd natural numbers is:",sum)

 

Output:

Enter the value of n: 3

The sum of the first 3 odd natural numbers is: 9


OR


Enter the value of n: 5

The sum of the first 5 odd natural numbers is: 25

No comments:

Post a Comment