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 sumConstraints:1<= input <= 10^18 Note: Dont use arrays or strings to solve this problem statementSample Input 1:123456Sample Output 1:9Sample Input 2:39605447885Sample Output 2:17

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 sumConstraints:1<= input <= 10^18 Note: Dont use arrays or strings to solve this problem statementSample Input 1:123456Sample Output 1:9Sample Input 2:39605447885Sample Output 2:17

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

Solution

To solve this problem, we need to find the absolute difference between the leftmost and rightmost digits of the given number. Then, we repeat this process for the second leftmost and second rightmost digits, and continue until we reach the middle digit.

Here are the step-by-step instructions to solve this problem:

  1. Accept an integer as input.
  2. Initialize a variable called "sum" to 0. This variable will store the sum of the differences.
  3. Create a while loop to iterate until we reach the middle digit.
  4. Inside the loop, find the leftmost digit by using the modulo operator (%) with a divisor of 10 raised to the power of the number of digits minus 1.
  5. Find the rightmost digit by using the modulo operator (%) with a divisor of 10.
  6. Calculate the absolute difference between the leftmost and rightmost digits.
  7. Add the absolute difference to the "sum" variable.
  8. Remove the leftmost and rightmost digits from the number by dividing it by 10 raised to the power of the number of digits minus 2.
  9. Repeat steps 4-8 until we reach the middle digit.
  10. If there is only one middle digit remaining, add it to the "sum" variable.
  11. Print the value of the "sum" variable as the output.

Please note that this solution does not use arrays or strings, as specified in the problem constraints.

This problem has been solved

Similar Questions

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

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).

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

Get two integers from user and print the absolute difference between two integersInput Format:Accept two integers as inputOutput Format:Print the absolute differenceConstraints:1<=N1,N2<=10^9Sample Input 1:10 20Sample Output 1:10Sample Input 2:78 25Sample Output 2:53

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.