Python program to find sum of n numbers; Through this tutorial, i am going to show you how to calculate or find sum of n numbers in python.
In this tutorial, i will write python programs to find or calculate sum of n numbers, sum of n even number and sum of n odd number using for loop, while loop and function.
Python Program Calculate Sum of n Numbers
See the following python programs to find or calculate sum of n numbers, sum of n even number and sum of n odd number using for loop, while loop and function:
- Python program to find sum of n numbers using for loop
- Python program to find sum of n numbers using while loop
- Python program to find sum of all numbers in list
- Python program to find sum of n odd numbers
- Python program to find sum of n even numbers
Python program to find sum of n numbers using for loop
Let’s see the following program to find sum of n numbers using for loop in python; as shown below:
n = input("Enter Number to calculate sum") n = int (n) sum = 0 for num in range(0, n+1, 1): sum = sum+num print("SUM of first ", n, "numbers is: ", sum )
Output:
Enter Number to calculate sum 5 Sum of first 5 number is: 15
Python program to find sum of n numbers using while loop
See the following program to find sum of n numbers using while loop in python; as shown below:
n = input("Enter Number to calculate sum") n = int (n) total_numbers = n sum=0 while (n >= 0): sum += n n-=1 print ("sum using while loop ", sum)
Output:
Enter Number to calculate sum 5 Sum using while loop 15
Python program to find sum of all numbers in list
Let’s see the following program to find sum of all numbers from list in python; as shown below:
sum = 0 list = [11,4,5,7,99,10,12] for num in list: sum = sum +num print ("sum of list element is : ", sum)
Output:
sum of list element is : 148
Python program to find sum of n odd numbers
n = input("Enter Number to calculate sum") n = int (n) sum = 0 for num in range(0, n+1, 1): if(not (num % 2) == 0): sum += num; print("SUM of odd numbers is: ", sum )
Output:
Enter Number to calculate sum 5 SUM of odd numbers is: 9
Python program to find sum of n even numbers
n = input("Enter Number to calculate sum") n = int (n) sum = 0 for num in range(0, n+1, 1): if((num % 2) == 0): sum += num; print("SUM of even numbers is: ", sum )
Output:
Enter Number to calculate sum 5 SUM of even numbers is: 9
Be First to Comment