0% found this document useful (0 votes)
122 views2 pages

11 12 Methods

The document contains definitions for 13 methods: 1) A method to calculate the factorial of a number. 2) A method to find the maximum of three numbers. 3) A method to count the digits in a number.

Uploaded by

subhash varghese
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
122 views2 pages

11 12 Methods

The document contains definitions for 13 methods: 1) A method to calculate the factorial of a number. 2) A method to find the maximum of three numbers. 3) A method to count the digits in a number.

Uploaded by

subhash varghese
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

Page 1 of 2

static int fact(int n){


int f=1; 1. Method to find factorial of a number
for(int i=1;i<=n;i++)
f=f*i;
return f;
}

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


int max;
max=a;

if(b> max) max=b;


if(c> max) max=c; 2. Method to find maximum of three numbers

return max;

static int countdigits(long n){


int c=0;

while(n>0){ 3. Method to find count of digits in a number


c=c+1;
n=n/10;
}
return c;
}

static long reversedigits(long n){


int c=0;
long temp=0;
4. Method to reverse a number
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); 5. Method to find sum of individual digits of a number
n=n/10;
}
return sum;
}

static long cubedigit(long n){


return n * n * n ; 6. Method to find cube of a number
}

static long sumofcube(long n){


long sum=0;
7. Method to find sum of cube of individual digits of a number
while(n>0){
long i=n%10;
sum=sum+cubedigit(i);
n/=10;
}
return sum;
}
Page 2 of 2

private static boolean isprime(long n){


boolean tf=true;

for(long i=2;i<=n/2;i++){
8. Method to check whether a given number is a prime number or not
if(n%i==0){
tf=false;
break;
}
}
return tf;
}

static boolean evenodd(int n){


if(n%2==0)
return true; 9. Method to check whether a given number is an even number or
else odd
return false;
}

static String sign(long n){


if(n<0)
return "Negative";
else if (n>0) 10. Method to check whether a given number is +ive, -ive or zero
return "Positive";
else
return "Zero";
}

} // end class

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


int min;
min=a;

if(b< min) min=b;


if(c> min) min=c; 11. Method to find minimum of three numbers

return min;
}

static int secondMax(int n1, int n2, int n3, int n4) {

int m1=n1; // to hold max value


int m2=n1; // to hold second max value 12. Method to find 2nd maximum of four numbers

if(n2>m1) m1=n2 ; else if(n2>m2) m2=n2 ;


if(n3>m1) m1=n3 ; else if(n3>m2) m2=n3 ;
if(n4>m1) m1=n4 ; else if(n4>m2) m2=n4 ;

return m2;
}

private static int sumPrime(long n){


long sum=0;
boolean p=true;

for( long k=1 ; k<=n ; k++) {


p=true ;
13. Method to find sum of first ‘n’ prime numbers
for(long i=2 ; i<=k/2 ; i++) {

if( k % i == 0){
p=false;
break;
} // end if
} // end inner for
if(p==true) sum=sum + k ;
} // end outer for
return sum;
}

You might also like