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.

Leave a comment

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