Python program to find sum of n even numbers using function

Even number

Even numbers are numbers that have a difference of 2 unit or number. In other words, if the number is completely divisible by 2 then it is an even number.

Sum of N even numbers

This program is much similar to this one: Print all even numbers from 1 to N. The only difference is that instead of printing them we have to add it to some temporary variable and print it.

Logic

First, we declare one variable ” sum ” with value 0, and then we are going to use this variable to store sum of all even numbers between 1 to N. Now after taking input [N] from user, we have to check if the current variable “i” is even or not inside the loop . If it is even we have to add it to variable ” sum ” otherwise continue with the loop.

Once the loop is completed we have to print the variable ” sum “ containing the sum of all even numbers up to N.

Program

# Take input from user.
num = int[input["Print sum of even numbers till : "]]

total = 0

for i in range[1, num + 1]:

    # Check for even or not.
    if[[i % 2] == 0]:
        total = total + i

print["\nSum of even numbers from 1 to", num, "is :", total]

Output

Print sum of even numbers till : 100
Sum of even numbers from 1 to 100 is : 2550

In this python tutorial, you will learn about Python program to find sum of n numbers and also we will check:

  • Python program to find sum of 3 numbers
  • Python program to find sum of n numbers using for loop
  • Python program to find sum of n numbers using a function
  • Python program to find sum of n numbers using while loop
  • python program to find sum of n numbers using recursion
  • Python program to find sum of n even numbers
  • Python program to find sum of n odd numbers
  • Python program to find sum of n prime numbers
  • Python program to find sum of first n numbers
  • Python program to find sum of first n even numbers
  • Python program to find sum of numbers in a list
  • Python program to find sum of numbers in a string
  • Python program to find sum of numbers in a file

Now, we can see how to find the sum of 3 numbers in python.

In this example, I have taken three inputs. The int data type is used and the “+” operator is used to find the sum of the three numbers.

Example:

number1 = input['Enter first number: ']
number2 = input['Enter second number: ']
number3 = input['Enter third number']
sum = int[number1] + int[number2] + int[number3]
print[sum]

We can see the sum of three inputs is 16 as the output. You can refer to the below screenshot for the output.

Python program to find the sum of 3 numbers

This is how to find sum of 3 numbers in Python.

You may like to read, How to print factorial of a number in Python and How to calculate simple interest in Python.

Python program to find sum of n numbers using for loop

Here, we can how to find the sum of n numbers using for loop in python.

  • In this example, I have taken an input. The int data type is used to sum only the integers.
  • Here, we can take an initial value sum = 0. The for loop is used for iteration number + 1 is used to increase the number up to the given input.
  • The sum = sum + value is used to find the sum.
  • To get the output, I have used print[sum].

Example:

number = int[input["Enter the Number: "]]
sum = 0
for value in range[1, number + 1]:
    sum = sum + value
print[sum]
 

We can see the sum of number till 10 is 55 as the output. You can refer to the below screenshot for the output.

Python program to find the sum of n numbers using for loop

This is how to find sum of n numbers using for loop in Python.

You may like Python For Loop with Examples

Python program to find sum of n numbers using a function

Here, we can how to find the sum of n numbers using a function in python.

  • In this example, I have taken an input. The function is defined as def sum[n].
  • The if condition is used if the input is less than 1 it returns n itself, if the number is greater than one the else condition is executed and then n is added to sum[n-1].
  • The print[“The sum is: “, sum[num]] is used to get the output.

Example:

num = int[input["Enter a number: "]]
def sum[n]:
    if n  0]:  
       sum += input  
       input -= 1  
   print["The result is",sum]  

The below screenshot shows the sum of numbers as the output.

python program to find sum of n numbers using while loop

The above code, we can use to find sum of n numbers using while loop in Python.

Check out While loop in Python.

Python program to find sum of n numbers using recursion

Here, we can see how to find sum of n numbers using recursion in python.

  • Python Recursion means calling the function itself.
  • In this example, I have defined a function as def recursion[n].
  • The if condition is used, if the number is less than 9 it should return the number itself.
  • If the number is greater than or equal to 9 it returns n + recursion[n – 1].
  • The print[recursion[n]] is used to get the output.

Example:

def  recursion[n]: 
	if n 

Chủ Đề