Program to check if the difference between sum of odd indexed digits and sum of even indexed digits is zero or not using Python

by | Jul 20, 2021 | Python Programs

Home » Python » Python Programs » Program to check if the difference between sum of odd indexed digits and sum of even indexed digits is zero or not using Python

Introduction

The task is to find whether the difference of sum of odd positioned digits and sum of even positioned digits is zero for a given number.

Program to check if the difference between sum of odd indexed digits and sum of even indexed digits is zero or not using Python

Program

def check(ip_no):
    if (ip_no % 11 == 0):
        print("The difference between sum of odd digits and even digits is zero!")
    else:
        print("The difference between sum of odd digits and even digits is not zero!")


ip_no = int(input("Enter the number: "))
check(ip_no)

Output

Program to check if the difference between sum of odd indexed digits and sum of even indexed digits is zero or not Output

Explanation

In the above code, the approach is: if the given number is divisible by 11, then the difference between the sum of even positioned and odd positioned digits is zero.

Another approach can be, by traversing and adding all the elements at odd position and traversing and adding all the elements at even position. At the end find the difference, and check if difference is equalled to zero.

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author