In the previous article, we have discussed Python Program to Check if Product of Digits of a Number at Even and Odd places is Equal
The task is to find the sum of digits of a number at even and odd places given a number N.
Examples:
Example1:
Input:
Given Number = 123456
Output:
The sum of all digits at even places in a given number{ 123456 } = 9 The sum of all digits at odd places in a given number { 123456 } = 12
Example2:
Input:
Given Number = 1787725620
Output:
The sum of all digits at even places in a given number{ 1787725620 } = 23 The sum of all digits at odd places in a given number { 1787725620 } = 22
Program to Count the Number of Odd and Even Digits of a Number at Even and Odd places in Python
Below are the ways to find the sum of digits of a number at even and odd places given a number N.
Method #1: Using For Loop (Static Input)
Approach:
- Give the number as static input and store it in a variable.
- Convert the given number to a string using the str() function and store it in another variable.
- Create a list of digits say “digtslst” using map(),list(),int functions.
- Store it in another variable.
- Take a variable say “evn_sum” and initialize it with 0.
- Take another variable say “od_sum” and initialize it with 0.
- Loop in the above list of digits until the length of the “digtslst” using the for loop.
- Check if the iterator value is even or not using the if conditional statement.
- If the statement is true, then add the iterator value of “digtslst” to the “evn_sum” and store it in the same variable evn_sum.
- If the statement is false, then add the iterator value of “digtslst” to the “od_sum” and store it in the same variable od_sum.
- Print “evn_sum“ to get the sum of even digits in a given number.
- Print “od_sum“ to get the sum of odd digits in a given number.
- The Exit of the Program.
Below is the implementation:
# Give the number as static input and store it in a variable. numb = 1787725620 # Convert the given number to string using the str() function. stringnum = str(numb) # Create a list of digits say "digtslst" using map(),list(),int functions. digtslst = list(map(int, stringnum)) # Take a variable say "evn_sum" and initialize it with 0. evn_sum = 0 # Take another variable say "od_sum" and initialize it with 0. od_sum = 0 # Loop in the above list of digits until the length of the "digtslst" using the for loop. for itr in range(len(digtslst)): # Check if the iterator value is even or not using # the if conditional statement. if(itr % 2 == 0): # If the statement is true, then add the iterator value of "digtslst" to the "evn_sum" # and store it in the same variable evn_sum. evn_sum += digtslst[itr] else: # If the statement is false, then add the iterator value of "digtslst" to the "od_sum" # and store it in the same variable od_sum. od_sum += digtslst[itr] # Print "evn_sum" to get the sum of all digits at even places in a given number. print( "The sum of all digits at even places in a given number{", numb, "} =", evn_sum) # Print "od_sum" to get the sum of all digits at odd places in a given number. print( "The sum of all digits at odd places in a given number {", numb, "} =", od_sum)
Output:
The sum of all digits at even places in a given number{ 1787725620 } = 23 The sum of all digits at odd places in a given number { 1787725620 } = 22
Method #2: Using For loop (User Input)
Approach:
- Give the number as user input using the int(input()) function and store it in a variable.
- Convert the given number to a string using the str() function and store it in another variable.
- Create a list of digits say “digtslst” using map(),list(),int functions.
- Store it in another variable.
- Take a variable say “evn_sum” and initialize it with 0.
- Take another variable say “od_sum” and initialize it with 0.
- Loop in the above list of digits until the length of the “digtslst” using the for loop.
- Check if the iterator value is even or not using the if conditional statement.
- If the statement is true, then add the iterator value of “digtslst” to the “evn_sum” and store it in the same variable evn_sum.
- If the statement is false, then add the iterator value of “digtslst” to the “od_sum” and store it in the same variable od_sum.
- Print “evn_sum“ to get the sum of even digits in a given number.
- Print “od_sum“ to get the sum of odd digits in a given number.
- The Exit of the Program.
Below is the implementation:
# Give the number as user input using the int(input()) function and store it in a variable. numb = int(input("Enter some random variable = ")) # Convert the given number to string using the str() function. stringnum = str(numb) # Create a list of digits say "digtslst" using map(),list(),int functions. digtslst = list(map(int, stringnum)) # Take a variable say "evn_sum" and initialize it with 0. evn_sum = 0 # Take another variable say "od_sum" and initialize it with 0. od_sum = 0 # Loop in the above list of digits until the length of the "digtslst" using the for loop. for itr in range(len(digtslst)): # Check if the iterator value is even or not using # the if conditional statement. if(itr % 2 == 0): # If the statement is true, then add the iterator value of "digtslst" to the "evn_sum" # and store it in the same variable evn_sum. evn_sum += digtslst[itr] else: # If the statement is false, then add the iterator value of "digtslst" to the "od_sum" # and store it in the same variable od_sum. od_sum += digtslst[itr] # Print "evn_sum" to get the sum of all digits at even places in a given number. print( "The sum of all digits at even places in a given number{", numb, "} =", evn_sum) # Print "od_sum" to get the sum of all digits at odd places in a given number. print( "The sum of all digits at odd places in a given number {", numb, "} =", od_sum)
Output:
Enter some random variable = 123456 The sum of all digits at even places in a given number{ 123456 } = 9 The sum of all digits at odd places in a given number { 123456 } = 12
Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.