Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 1

*** A user defined class ‘MyMath’ with various mathematical functions created by the user.

Note that all


functions are declared to be static and thus they can be directly qualified with the class name i.e. you can
call fact function as MyMath.fact(23)

public class MyMath {

static int fact(int n){


int f=1;
for(int i=1;i<=n;i++)
f=f*i;
return f;
}

static int findmax(int a,int b, int c){


int max;

if(a>b && a>c)


max=a;
else if(b>a && b>c)
max=b;
else
max=c;

return max;
}

static int countdigits(long n){


int c=0;

while(n>0){
c=c+1;
n=n/10;
}
return c;
}

static long reversedigits(long n){


int c=0;
long temp=0;

while(n>0){
temp=(temp*10)+(n%10);
n=n/10;
}
return temp;
}

static long adddigits(long n){


long sum=0;

while(n>0){
sum=sum+(n%10);
n=n/10;
}
return sum;
}

} // end class

You might also like