Knowee
Questions
Features
Study Tools

In a mobile recharge system, users enter a short voucher code to recharge their balance. You are supposed to write a program to validate the voucher by calculating the sum of even and odd digits separately to ensure the code is valid.If the sum of even and odd digits are equal the code is considered valid, otherwise it is not.Input format :The input consists of a short integer value n, representing the voucher code.Output format :The first line prints "Sum of even digits: " followed by the sum of even digits of n.The second line prints "Sum of odd digits: " followed by the sum of odd digits of n.The third line prints "Valid" if the sum of even and odd digits are equal, otherwise it prints "Not valid".

Question

In a mobile recharge system, users enter a short voucher code to recharge their balance. You are supposed to write a program to validate the voucher by calculating the sum of even and odd digits separately to ensure the code is valid.If the sum of even and odd digits are equal the code is considered valid, otherwise it is not.Input format :The input consists of a short integer value n, representing the voucher code.Output format :The first line prints "Sum of even digits: " followed by the sum of even digits of n.The second line prints "Sum of odd digits: " followed by the sum of odd digits of n.The third line prints "Valid" if the sum of even and odd digits are equal, otherwise it prints "Not valid".

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a Python solution for the problem:

def validate_voucher(n):
    # Convert the integer to a string to be able to iterate over the digits
    n = str(n)
    
    # Initialize the sums of even and odd digits
    sum_even = 0
    sum_odd = 0
    
    # Iterate over the digits
    for digit in n:
        # Convert the digit back to an integer
        digit = int(digit)
        
        # Check if the digit is even or odd and add it to the corresponding sum
        if digit % 2 == 0:
            sum_even += digit
        else:
            sum_odd += digit
    
    # Print the sums
    print("Sum of even digits: ", sum_even)
    print("Sum of odd digits: ", sum_odd)
    
    # Check if the sums are equal and print the result
    if sum_even == sum_odd:
        print("Valid")
    else:
        print("Not valid")

# Test the function
validate_voucher(123456)

This program works by first converting the input integer to a string so that we can iterate over its digits. It then initializes two variables to keep track of the sum of the even and odd digits. It iterates over the digits, checking if each one is even or odd by using the modulus operator, and adds it to the appropriate sum. Finally, it prints the sums and checks if they are equal, printing "Valid" if they are and "Not valid" if they are not.

This problem has been solved

Similar Questions

Input format :The input consists of a 'long long' integer, representing the 10-digit mobile number.Output format :The output prints "Weighted sum of digits: " followed by an integer representing the weighted sum.

Alex is teaching a class on number properties and wants to create an exercise for his students. He needs a program that takes a three-digit number as input, calculates the sum of its digits, and determines whether the sum is even or odd.

write a program to check whether a number is a special 2-digit number or not. A Special 2-digit number is such that when the sum of its digits is added to the products of its digits the result is equal tot he original 2-digit number

James, a mathematics teacher, is developing a programming exercise to help his students practice continuously summing the digits of a number until it becomes a single-digit integer. He wants to create a simple program using a 'while' loop that takes a positive integer input and generates the final single-digit result. Input format :The input consists of a positive integer n.Output format :The output prints "The single-digit sum of n is Y." where n is the input integer and Y is its single-digit sum.Refer to the sample output for formatting specifications.Code constraints :1 ≤ n ≤ 1010Sample test cases :Input 1 :456Output 1 :The single-digit sum of 456 is 6.Input 2 :999Output 2 :The single-digit sum of 999

Alex is creating a utility to evaluate a user's numerical input. The program calculates the absolute difference between the sum of digits at odd and even positions, where the first digit is considered position 1. Users enter a sequence of digits, and the program outputs the result.ExampleInput:5674Output:2Explanation:The sum of the even-position digits 6 and 4 is 10. In the odd position, the sum of digits 5 and 7 is 12. So, the absolute difference is |10-12| = 2.Input format :The input consists of an integer n.Output format :The output prints the difference between the sum of the odd and even position digits in the given integer.Refer to the sample output for the formatting specifications.Code constraints :In the given scenario, the test cases will fall under the following constraints:10 ≤ n ≤ 107Sample test cases :Input 1 :5674Output 1 :2Input 2 :1234567Output 2 :4Input 3 :10Output 3 :1Input 4 :10000000Output 4 :1

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.