135. JAVA Program to find Sum of given number upto single digit using recursion.
How does this program work?
- This program is used to find Sum of given number upto single digit using recursion in java.
Here is the code
//To find the Sum of given number upto single digit using recursion
import java.util.Scanner;public class digit
{
public static void main(String args[] )
{
int num;
int dig,sum;
System.out.println("Enter number : ");
Scanner obj=new Scanner(System.in );
num=obj.nextInt();
System.out.println(+num );
//Using Do while loop finding sum
do{
sum = 0;
while(num!=0)
{
//compute sum of digits
dig=num%10;sum+=dig;
num/=10;
}
//display final answer
System.out.println("sum of a number is:");System.out.println(+sum);
num=sum;
}
//Loop upto sum is unit digit
while (num/10!=0);}
}