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.
13 Réponses
+ 3
The easiest way is to convert the number into an array of characters, take the substring and then find the sum.
+ 2
i have tried but no use
can you provide me with complete code
+ 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;
}
+ 1
Can you suugest me with code
0
Post your attempt
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;
}
0
thanx a lot
0
Hey needed c++
- 1
Use while loop to get number of digits
- 1
i dont know to use while loop
that is why i request you to provide code
- 1
- 1
Am new here who can teach me python directly



