Input: 253
Output: 10
(we need to add the individual digits in the number)
Source code:
import java.util.*;
import java.io.*;
public class Main
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.println("Enter number");
Long n =s.nextLong();
System.out.println("Answer: "+sumOfDigits(n));
}
public static Long sumOfDigits(Long n)
{
if(n==0)
return 0L;
return (n%10)+sumOfDigits(n/10);
}
}
Output:
Enter number
1155588
Answer: 33
No comments:
Post a Comment