Iam trying to solve a question.The question is.Finding sum of digits of a number until sum becomes single digit.
Example
Input : 12345
Output: 6
The individual digits should be added untill it become single digit.1+2+3+4+5=15 -->1+5=6.
In the below code the input is:
i)first line number of test cases
ii)second line is the number.
When I run the code , the following are the inputs Iam giving:
1;
12345;
The output should be "6" but it is printing "15".It is not printing "6".Iam unable to understand what's wrong woth the below code.Plz. help!!
import java.util.Scanner; class AddNum{ public int Add(int num) { int x; int sum=0; int rem; while(num>0) { rem=num%10; sum=sum+rem; num/=10; } if(sum>9) { Add(sum); } return sum; } } class IndividSum { public static void main(String[] args)throws Exception{ Scanner sc=new Scanner(System.in); int test=sc.nextInt(); AddNum obj=new AddNum(); int num[]=new int[test]; int Ans[]=new int[test]; for(int i=0;i<test;i++) { num[i]=sc.nextInt(); } for(int i=0;i<test;i++) { Ans[i]= obj.Add(num[i]); System.out.println(Ans[i]); } sc.close(); } }