Posts

Showing posts from August, 2020

Finding the sum of all even numbers upto a given number

How to find the sum of all even numbers upto a given number in Python ? To find the sum of all even numbers upto a given number we can use the following python code.  #taking a number from user upto which sum is to be find . n = int(input("enter a number")) #setting the base value of sum sum = 0 #setting the range for i in range (1,n+1): #checking for even number in range if i % 2 == 0: #incrementing the value of sum sum = sum + i #printing sum print (sum)

FINDING FACTORIAL OF A NUMBER IN PYTHON

How to find factorial of a number using python. To find the factorial of a number in python we have two main methods as follows : 1. Using for loop in python 2. Using recursion 1. Using for loop in python By using python for loop we can find the factorial of a number as follows :