The following article shows how given an integer list, we can produce the sum of all its odd and even digits. Show Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation. Input : test_list = [345, 893] Output : Odd digit sum : 20 Even digit sum : 12 Explanation : 4 + 8 = 12, even summation. Method 1 : Using loop, str() and int() In this, we first convert each element to string and then iterate for each of its element, and add to respective summation by conversion to integer. Python3
Output The original list is : [345, 893, 1948, 34, 2346] Odd digit sum : 36 Even digit sum : 40 Method 2: Using loop and sum() In this, we perform task of getting summation using sum(), and loop is used to perform the task of iterating through each element. Python3
Output The original list is : [345, 893, 1948, 34, 2346] Odd digit sum : 36 Even digit sum : 40 Method 3: Using list comprehension Python3
Output Odd digit sum : 36 Even digit sum : 40 Method 4: Using the enumerate function Python3
Output Odd digit sum : 36 Even digit sum : 40 How do you find the sum of all even numbers in Python?Python program to calculate sum of even numbers using for loop. In the given program, we first take user input to enter the maximum limit value. Then, we have used the for loop to calculate the sum of even numbers from 1 to that user-entered value.
How do you print the sum of even numbers?Program: Write a program to find the sum of even numbers in C language.. #include <stdio.h>. int main(). int i, n, sum=0;. printf("Enter any number: ");. scanf("%d", &n);. for(i=2; i<=n; i+=2). How do you print the sum of all the even numbers in the range entered by the user?Example #1: Print all even numbers from given list using for loop Define start and end limit of range. Iterate from start till the range in the list using for loop and check if num % 2 == 0. If the condition satisfies, then only print the number.
How do you print the sum of even and odd numbers in Python?Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation. Input : test_list = [345, 893] Output : Odd digit sum : 20 Even digit sum : 12 Explanation : 4 + 8 = 12, even summation.
|