Digit sum

 Digit sum

Problem Description


Digit Sum sum of all digits of a number


Input :129


Output:3


Explanation 1+2+9=12,


1+2=3

Logic Test Case 1


Input (stdin)

2512


Expected Output


1

Logic Test Case 2


Input (stdin)

9009


Expected Output


9




CODE AREA

import java.io.*;

import java.util.Scanner;

public class TestClass {

     public static void main(String[] args) { 

        int  n, sum= 0;

        Scanner s = new Scanner(System.in);

        n= s.nextInt();

while (n > 0) 

int p = n % 10; 

sum = sum + p; 

n = n / 10; 

if(n==0 && sum>9)

{

n=sum;

sum=0;

}}

        System.out.println(sum);

     }

}

Post a comment

0 Comments