Sunday, October 20, 2019

Sum of digits of a number



Program  

package sumofdigits;

public class SumOfDigits
{
    public static void main(String[] args)
    {
        int n = 34;
        int sum = 0;
        while(n > 0)
        {
            int temp = n % 10;
            sum = sum + temp;
            n = n / 10;
        }
        System.out.println(sum);
    }
   
}

Video  explanation



2 comments: