0

How can we find the sum of middle numbers in four digit number using python ?

For example take 2345, how we add middle numbers 3 and 4 and gives output ..the sum of middle numbers is 7.

20th Aug 2019, 5:17 AM
Renuka Velangini
Renuka Velangini - avatar
4 Respostas
+ 4
The simplest way to do this would be to convert the number into a string, crop the string so it was left with only the middle digits, then (having a <sum> variable at hand) you can have each character of the cropped string (converted as number) be added to the <sum> variable. There are probably easier ways to go with Python I wasn't aware of, since I don't know much Python : ) Anyways, are you also looking forward to do it in C? because I see you tagged that language with the question.
20th Aug 2019, 5:37 AM
Ipang
+ 3
you can separate em, and store them inside a list, then you can do the rest
20th Aug 2019, 5:29 AM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 1
num=int(input()) string=str(num) if (len(string)%2==1): print('The middle number of %d-digit number,%d is '%(len(string),num)+string[int((len(string)+1)/2-1)]) else: print('The sum of two middle numbers of %d-digit number,%d is '%(len(string),num),end='') print(int(string[int(len(string)/2-1)])+int(string[int(len(string)/2)]))
20th Aug 2019, 5:37 AM
Ketchup🍅
Ketchup🍅 - avatar
+ 1
Thank you sooooo much......I got output
20th Aug 2019, 6:38 AM
Renuka Velangini
Renuka Velangini - avatar