Knowee
Questions
Features
Study Tools

Given a number find the absolute difference between left most and right most digit, and repeat it for second from left and second from right, and do repeat it until middle digit is met. Find the sum of those differences and if only one middle digit is available do add it to sum.Input Format:Accept an integer as an inputOutput Format:Print the calculated sum

Question

Given a number find the absolute difference between left most and right most digit, and repeat it for second from left and second from right, and do repeat it until middle digit is met. Find the sum of those differences and if only one middle digit is available do add it to sum.Input Format:Accept an integer as an inputOutput Format:Print the calculated sum

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

Solution

Here is a step-by-step solution in Python:

  1. First, convert the input number into a string to easily access each digit:
num = str(input())
  1. Initialize a variable to store the sum of the differences:
sum_diff = 0
  1. Use a loop to iterate over the digits from both ends towards the middle. The range of the loop is half the length of the number string. If the length of the number string is odd, the middle digit will be included in the loop:
for i in range(len(num)//2):
  1. In each iteration, calculate the absolute difference between the digit at the current index from the left (i) and the digit at the corresponding index from the right (-i-1). Add this difference to the sum:
    sum_diff += abs(int(num[i]) - int(num[-i-1]))
  1. If the length of the number string is odd, add the middle digit to the sum:
if len(num) % 2 != 0:
    sum_diff += int(num[len(num)//2])
  1. Finally, print the sum of the differences:
print(sum_diff)

Here is the complete code:

num = str(input())
sum_diff = 0
for i in range(len(num)//2):
    sum_diff += abs(int(num[i]) - int(num[-i-1]))
if len(num) % 2 != 0:
    sum_diff += int(num[len(num)//2])
print(sum_diff)

This problem has been solved

Similar Questions

Given a positive integer 'x' (with even number of digits in it), write an algorithm and the subsequent code to compute the difference between  the sum of the digits occuring in the alternate positions (starting from the first position) and the sum of the digits occuring in the alternate positions,starting from the last rightmost position of 'x'For example, consider the number  8975.  The sum of the digits that occur in the alternate positions from the first position is 8+7=15.  The sum of the digits that occur in the alternate positions, starting from the rightmost position is 5+9 = 14. Difference between the two sums is 1 (=15-14).  Similarly, for the number 5798, the difference between  two sums, is 1.  Note: Read the input as a number and do entire processing as  a numberC++ compilers can compile C code alsoInput format First line contains the positive integerOutput format :First line should contain the difference between  the sum of the digits occuring in the alternate positions (starting from the first position) and the sum of the digits occuring in the alternate positions (startting from the last rightmost position).

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

Problem StatementVivek wants to count the number of digits in the given integer and find the sum of the first and last digits of the number. Write a suitable program to complete the above task using a for loop.ExampleInput:1221Output:Sum = 2Digits = 4Explanation:Counting Digits: The number 1221 has 4 digits.First and Last Digits: The last digit is 1 (1221 % 10). To find the first digit, divide 1221 by 10 repeatedly until it's less than 10. The first digit is 1.The sum is 1 + 1 = 2.Note: This question helps in clearing coding technical tests for service-based companies.Input format :The input consists of an integer n.Output format :The first line prints "Sum = " followed by an integer which is the sum of the first and last digits.The second line prints "Digits = " followed by an integer which is the number of digits.Refer to the sample output for the formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:10 ≤ n ≤ 10000Sample test cases :Input 1 :1221Output 1 :Sum = 2Digits = 4Input 2 :10Output 2 :Sum = 1Digits = 2Input 3 :10000Output 3 :Sum = 1Digits = 5

A three digit number is called a sandwich number if the sum of its first and last digit is equal to its middle digit. Accept a three digit number as input and print sandwich if the number is a sandwich number. Print plain if the number is not a sandwich number. Select the correct implementation of the code that achieves this.1num = int(input())2first, middle, last = num[0], num[1], num[2]3if first + last == middle:4 print('sandwich')5else:6 print('plain')1num = input()2first, middle, last = num[0], num[1], num[2]3if first + last == middle:4 print('sandwich')5else:6 print('plain')1num = input()2first, middle, last = int(num[0]), int(num[1]), int(num[2])3if first + last == middle:4 print('sandwich')5else:6 print('plain')1num = int(input())2first, middle, last = int(num[0]), int(num[1]), int(num[2])3if first + last == middle:4 print('sandwich')5else:6 print('plain')

Problem Statement:Write a  program to find the sum of numbers in the given range.Input Format:Given two integers start and end ranges separated by SPACE.Output Format:Print the sum in the given range.Constraints1<=start < end <= 1000000NOTE : The calculated sum value might exceed integer range.Sample Input 1:10 20Sample Output 1:165Sample Input 2:1 100Sample Output 2:5050

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.