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 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 /* 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
Given a number, find the sum of its digits.
Examples :
Input: n = 687
Output: 21Input: n = 12
Output: 3
Follow the below steps to solve the problem:
- Get the number
- Declare a variable to store the sum and set it to 0
- Repeat the next two steps till the number is not 0
- Get the rightmost digit of the number with help of the remainder ‘%’ operator by dividing it by 10 and adding it to the sum.
- Divide the number by 10 with help of ‘/’ operator to remove the rightmost digit.
- Print or return the sum
Below is the implementation of the above approach:
C++
#include
using
namespace
std;
class
gfg {
public
:
int
getSum[
int
n]
{
int
sum = 0;
while
[n != 0] {
sum = sum + n % 10;
n = n / 10;
}
return
sum;
}
};
int
main[]
{
gfg g;
int
n = 687;
cout
Javascript
function
getSum[n]
{
var
sum = 0;
while
[n != 0] {
sum = sum + n % 10;
n = parseInt[n / 10];
}
return
sum;
}
var
n = 687;
document.write[getSum[n]];
Time Complexity: O[log N]
Auxiliary Space: O[1]
How to compute in a single line?
The below function has three lines instead of one line, but it calculates the sum in one line using for loop. It can be made one-line function if we pass the pointer to the sum.
Below is the implementation of the above approach:
C++
#include
using
namespace
std;
class
gfg {
public
:
int
getSum[
int
n]
{
int
sum;
for
[sum = 0; n > 0; sum += n % 10, n /= 10]
;
return
sum;
}
};
int
main[]
{
gfg g;
int
n = 687;
cout 0; sum += n % 10, n /= 10]
;
return
sum;
}
int
main[]
{
int
n = 687;
printf
[
" %d "
, getSum[n]];
return
0;
}
Java
import
java.io.*;
class
GFG {
static
int
getSum[
int
n]
{
int
sum;
for
[sum =
0
; n >
0
; sum += n %
10
, n /=
10
]
;
return
sum;
}
public
static
void
main[String[] args]
{
int
n =
687
;
System.out.println[getSum[n]];
}
}
Python3
def
getSum[n]:
sum
=
0
while
[n >
0
]:
sum
+
=
int
[n
%
10
]
n
=
int
[n
/
10
]
return
sum
if
__name__
=
=
"__main__"
:
n
=
687
print
[getSum[n]]
C#
using
System;
class
GFG {
static
int
getSum[
int
n]
{
int
sum;
for
[sum = 0; n > 0; sum += n % 10, n /= 10]
;
return
sum;
}
public
static
void
Main[]
{
int
n = 687;
Console.Write[getSum[n]];
}
}
PHP
Javascript
function
getSum[n]
{
let sum;
for
[sum = 0; n > 0;
sum += n % 10,
n = parseInt[n / 10]]
;
return
sum;
}
let n = 687;
document.write[getSum[n]];
Time Complexity: O[log N]
Auxiliary Space: O[1]
Sum of the digits of a given number using recursion:
Follow the below steps to solve the problem:
- Get the number
- Get the remainder and pass the next remaining digits
- Get the rightmost digit of the number with help of the remainder ‘%’ operator by dividing it by 10 and adding it to the sum.
- Divide the number by 10 with help of the ‘/’ operator to remove the rightmost digit.
- Check the base case with n = 0
- Print or return the sum
Below is the implementation of the above approach:
C++
#include
using
namespace
std;
class
gfg {
public
:
int
sumDigits[
int
no]
{
if
[no == 0] {
return
0;
}
return
[no % 10] + sumDigits[no / 10];
}
};
int
main[
void
]
{
gfg g;
cout
Javascript
function
sumDigits[no]
{
if
[no == 0]{
return
0 ;
}
return
[no % 10] + sumDigits[parseInt[no/10]] ;
}
document.write[sumDigits[687]];
Time Complexity: O[log N]
Auxiliary Space: O[log N]
Sum of the digits of a given number with input as string:
When the number of digits of that number exceeds 1019 , we can’t take that number as an integer since the range of long long int doesn’t satisfy the given number. So take input as a string, run a loop from start to the length of the string and increase the sum with that character[in this case it is numeric]
Follow the below steps to solve the problem:
- Declare a variable sum equal to zero
- Run a loop from zero to the length of the input string
- Add the value of each character into the sum, by converting the character into it’s integer value
- Return sum
Below is the implementation of the above approach:
C++14
#include
using
namespace
std;
int
getSum[string str]
{
int
sum = 0;
for
[
int
i = 0; i < str.length[]; i++] {
sum = sum + str[i] - 48;
}
return
sum;
}
int
main[]
{
string st =
"123456789123456789123422"
;
cout
Javascript
function
getSum[str]
{
let sum = 0;
for
[let i = 0; i < str.length; i++]
{
sum = sum + parseInt[str[i]];
}
return
sum;
}
let st =
"123456789123456789123422"
;
document.write[getSum[st]];
Time Complexity: O[log N]
Auxiliary Space: O[1]
Sum of the digits of a given number using tail recursion:
Follow the below steps to solve the problem:
- Add another variable “Val” to the function and initialize it to [ Val = 0 ]
- On every call to the function add the mod value [n%10] to the variable as “[n%10]+val” which is the last digit in n. Along with passing the variable n as n/10.
- So on the First call, it will have the last digit. As we are passing n/10 as n, It follows until n is reduced to a single digit.
- n