Write a Method to calculate and return the sum of the square of the digits of a number
'n' using recursive technique.
The method declaration is as follows:
int sumSq(int n)
Answers
Method to calculate and return the sum of the square of the digits of a number 'n' using recursive technique.
Explanation:
- The method is similar to a function in object-oriented programming to which parameters are passed and the operation is [performed and the result is returned.
- A class can have any number of methods.
- The method can only the data known to its object.
- In this method, the number is passed as in input through the variable n.
- If the number is a single digit, it is squared and returned directly.
- If the number has more than one digit, then modulo operation is performed to separate the digits and then it is squared.
public static int int sumSq(int n)
{
if (n < 10) return n^2;
return (((n % 10)^2) +sumSq(n/10));
}
Learn more about methods:
Write a java program to compute the amount that has to be paid to a person (employee) using the following rules for calculating tax.
https://brainly.in/question/15584326
What is the statement specifically called that involves a method in java
https://brainly.in/question/13385154
import java.util.*;
public class recursive
{
int SumSq(int n)
{
if(n<10)
{
return n*n;
}
else
{
return(((n%10)*(n%10))+SumSq(n/10));
}
}
public static void main(String args[])
{
recursive obj = new recursive();
Scanner in = new Scanner(System.in);
System.out.println("enter the number");
int number = in.nextInt();
int ans = obj.SumSq(number);
System.out.println(ans);
}
}