0
Find the difference between the sum of digits in a number at odd places and sum of digits at even places.
Eg. 7345 i.e.( 7+4) -(3+5) Output : 3 Entered No. Can be maximum of 100 digits . Language: Python (Python is the priority,other languages can also be used) This question was asked in TCS TNQT 2019
4 ответов
+ 5
It would be best if you shared your attempt first by writing it in the Code Playground and posting a link to it here.
I'm assuming you are using the popular concept of calling the first number's place 1, i.e. odd, rather than its index position 0, which is even.
n = input() or "7345"
# Here "positions" begin at 1 rather than index position 0,
# so even index positions mean odd positions here.
if len(n) > 100:
print("Only 100 digits allowed.")
quit()
sum_even_positions = sum(int(n[i]) for i in range(len(n)) if i % 2)
sum_odd_positions = sum(int(n[i]) for i in range(len(n)) if not i % 2)
print(sum_odd_positions - sum_even_positions)
https://code.sololearn.com/cUBaChEV0ZMR
+ 3
Find my attempt to ur question...
https://code.sololearn.com/cHMMw4DzC4cJ/?ref=app
Hope this helps...!!!
+ 1
And what is your question about this? If you need help to write this program, then show your attempt first, and explain where you are stuck.
0
#Used my own Variables in the code
University=input("Enter the number")
even_sum=0
odd_sum=0
University_length=len(University)
for student in range(University_length):
if student %2==0:
even_sum+=int(University[student])
else:
odd_sum+=int(University[student])
if even_sum>odd_sum:
print("The difference of odd and even position digits sum is",even_sum-odd_sum)
else:
print("The difference of odd and even position digits sum is",odd_sum-even_sum)