fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. Scanner in=new Scanner(System.in);
  14. int n=in.nextInt();
  15. int k=in.nextInt();
  16. int x=sum_digit(n,k);
  17. System.out.print(x);
  18. }
  19. public static int sum_digit(int a, int b){
  20. int p=a;
  21. int count=0;
  22. while(p>0){
  23. count++;
  24. p=p/10;
  25. }
  26. int s=a;
  27. int[] arr=new int[count];
  28. int sum=0;
  29. for(int i=0;i<=b;i++){
  30. int d=s%10;
  31. s=s/10;
  32. arr[i]=d;
  33. sum=sum+arr[i];
  34. }
  35. return sum;
  36. }
  37. }
Success #stdin #stdout 0.13s 47464KB
stdin
56789
0
stdout
9