FaltuTech.Club : Fane of Advanced Logical Thriving Utopian Technical Club

Find Sum of Digits of Number Simple and Recursive in Java

Find Sum of Digits of Number Simple and Recursive in Java

Simple



class SimpleSum{
    public static void main(String args[]){
	int Int = 475849;

	int temp = Int;
	int sum=0;

	while(temp!=0){
		
	sum += temp%10;
	temp /= 10;

	}

	System.out.println("Sum of Digits is " + sum);
	
    }
}


Recursive



class RecurSum{
	private int Int = 475849;
	private int recursiveSum(int x){

	if(x==0)
	return 0;
	
	int sum = recursiveSum(x/10) + x%10;
	return sum;

	}

	public void example(){
	
	System.out.println(recursiveSum(Int));

	}
}