0

This the the function that store the value in sum for digitsum of all n digit number

void findsum(int pos,int n,int& sum)
{
    if(pos>n){
    return;
    }

    for (int i = 0; i < 10; i++)
    {   
        findsum(pos+1,n,sum);
        sum+=i;
    }
    
}

This is the driver function

int main()
{
    int n;
    cin>>n;
    int sum=0;
    findsum(1,n,sum);
    cout<<sum;

}

1 Answers1

0

You wanted the output as the sum of the digits that you are giving as input.

Input

Enter the Number:1234

Output

10

Here is your code

import java.io.*;
class Sum {
    static int getSum(int n)
    {
        int sum = 0;
 
        while (n != 0) {
            sum = sum + n % 10;
            n = n / 10;
        }
 
        return sum;
    }
    public static void main(String[] args)
    {
        int n ;
        cin>>n;
        System.out.println(getSum(n));
    }
}
Nishant Bangera
  • 176
  • 1
  • 1
  • 8