For example take 2345, how we add middle numbers 3 and 4 and gives output ..the sum of middle numbers is 7.
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.
you can separate em, and store them inside a list, then you can do the rest
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)]))
Thank you sooooo much......I got output
For example take 2345, how we add middle numbers 3 and 4 and gives output ..the sum of middle numbers is 7.
xavier This code works for 4 digit number. If you want code that works for any digit, you can tell me. I will give you. #include <stdio.h> int main() { int a,sum; printf("Enter a number: "); scanf("%d",&a); sum=(a/10)%10+(a/100)%10; printf("Answer: %d",sum); return 0; }
The easiest way is to convert the number into an array of characters, take the substring and then find the sum.
i have tried but no use can you provide me with complete code
If you are sure it is a four digit number then #include <stdio.h> /* Function to get sum of digits */ int getSum(int n) { int sum; sum=0; /* Single line that calculates sum */ sum=sum+(n/100)%10+(n%100)/10; return sum; } // Driver code int main() { int n = 3547; printf(" %d ", getSum(n)); return 0; }
Can you suugest me with code
/* Function to get sum of digits */ int getSum(int n) { int sum; /* Single line that calculates sum */ for (sum = 0; n > 0; sum += n % 10, n /= 10) ; return sum; } // Driver code int main() { int n = 3547; printf(" %d ", getSum(n)); return 0; }
Use while loop to get number of digits
i dont know to use while loop that is why i request you to provide code
Am new here who can teach me python directly
33. Python Program to Check Middle digit of given 3 digit number is Equal to the Sum of Other Two digits or Not.
How does this program work?
- In this program you are going to learn about how to Check Middle digit of given 3 digit number is Equal to the Sum of Other Two digits or Not using Python.
- Here First we claculate Last digit, First digit and their Sum.
- After that By using Mathematical Calculations like logarithm then compare Sum and middle digit using if condition and display result.
Here is the code
//To Check middle digit is Equal to the Sum of Other Two Digits or Not
a = int(input("Enter a three digit number: "))
n = a % 10
a = int(a / 10)
b = a % 10
a = int(a / 10)
if b == (n + a):
print("Middle digit is numerically equal to the sum of other two digits")
else:
print("Middle digit is not numerically equal to the sum
of other two digits")