How can we find the sum of middle numbers in four digit number using C programming ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

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

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

28th Apr 2021, 5:03 AM
xavier
13 Answers
+ 4
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; }
29th Apr 2021, 7:51 AM
MrMysterious5
+ 3
The easiest way is to convert the number into an array of characters, take the substring and then find the sum.
29th Apr 2021, 8:40 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 2
i have tried but no use can you provide me with complete code
28th Apr 2021, 5:36 AM
xavier
+ 2
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; }
28th Apr 2021, 6:51 AM
Atul [Inactive]
+ 1
Can you suugest me with code
28th Apr 2021, 5:28 AM
xavier
0
Post your attempt
28th Apr 2021, 5:47 AM
Atul [Inactive]
0
/* 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; }
28th Apr 2021, 6:12 AM
xavier
0
thanx a lot
28th Apr 2021, 7:38 AM
xavier
0
Hey needed c++
29th Apr 2021, 6:47 PM
jonathan kaspersky
jonathan kaspersky - avatar
- 1
Use while loop to get number of digits
28th Apr 2021, 6:20 AM
Atul [Inactive]
- 1
i dont know to use while loop that is why i request you to provide code
28th Apr 2021, 6:22 AM
xavier
- 1
28th Apr 2021, 12:00 PM
xavier
- 1
Am new here who can teach me python directly
29th Apr 2021, 8:54 AM
Olohunlomerue Mujeeb
Olohunlomerue Mujeeb - avatar