Design a CLASS to overload a function sum( ) as follows:
(i) int sum(int A, int B) – with two integer arguments (A and B) calculate and return sum of all the even numbers in the range of A and B.
Sample input: A=4 and B=16
Sample output: sum = 4 + 6 + 8 + 10 + 12 + 14 + 16
(ii) double sum( double N ) – with one double arguments(N) calculate and return the product of the following series:
sum = 1.0 x 1.2 x 1.4 x …………. x N
(iii) int sum(int N) – with one integer argument (N) calculate and return sum of only odd digits of the number N.
Sample input : N=43961
Sample output : sum = 3 + 9 + 1 = 13
Write the main method to create an object and invoke the above methods.
public class methodoverload
{
int sum(int a,int b)
{
int tsum=0;;
for(int i=a;i<=b;i++){
if(i%2==0)
tsum=tsum+i;
else
tsum=tsum;
}
return tsum;
}
double sum(double n)
{
double pro=1.0;
for(double i=1.0;i<=n;i+=0.2){
pro=pro*i;
System.out.print(i +" * ");
}
return pro;
}
int sum(int n)
{
int s=0;
int d;
while(n>0)
{
d=n%10;
s=s+d;
n=n/10;
}
return s;
}
public static void main(String args[])
{
methodoverload m=new methodoverload();
System.out.println(m.sum(2,10));
System.out.println(m.sum(123));
System.out.println(m.sum(1.5));
}
}