0% found this document useful (0 votes)
56 views

11 12 Methods 2

The document contains definitions for 6 static methods: 1. A method to calculate the sum of the first n even numbers 2. A method to calculate the sum of the first n odd numbers 3. A method to calculate the sum of the squares of the digits in a number 4. A method to calculate the sum of the divisors of a number 5. A method to reverse a string 6. A method to check if a string is a palindrome by reversing it and comparing to the original string

Uploaded by

subhash varghese
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

11 12 Methods 2

The document contains definitions for 6 static methods: 1. A method to calculate the sum of the first n even numbers 2. A method to calculate the sum of the first n odd numbers 3. A method to calculate the sum of the squares of the digits in a number 4. A method to calculate the sum of the divisors of a number 5. A method to reverse a string 6. A method to check if a string is a palindrome by reversing it and comparing to the original string

Uploaded by

subhash varghese
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 1

Page 1 of 1

static int sumEven(int n){


int sum=0; 1. Method to find sum of first ‘n’ even numbers
for(int i=1; i<=n ;i++)
if ( i % 2==0) sum=sum + i ;

return sum;
}

static int sumOdd(int n){


int sum=0;
for(int i=1; i<=n ;i++)
if ( i % 2 !=0) sum=sum + i ; 2. Method to find sum of first ‘n’ odd numbers

return sum;
}

static long adddigits(long n){


long sum=0;
int dig=0;

while(n>0){
dig=n%10; 3. Method to find sum of square of individual digits of a number
sum=sum+(dig * dig);
n=n/10;
}
return sum;
}

static long sumDivisors(long n){


long sum=0;
4. Method to find sum of divisors of a number
for( int i=1 ; i <=n ; i++) {
if( n % i == 0)
sum=sum + i ;

}
return sum;
}

static String reverseString(String s){


int n=s.length();
String nws=""; 5. Method to reverse a string

for(int i=n-1;i>=0;--i){
nws=nws+s.charAt(i);
}

return nws ;
}

static boolean palin(String s){


int n=s.length();
String nws="";

for(int i=n-1;i>=0;--i){ 6. Method to find whether a given String is ‘Palindrome’ or


nws=nws+s.charAt(i); Not, Palindrome string is a string which reads the same
} from left and Right. Ex. ‘dad’, ‘mom’

if(s.equalsIgnoreCase(nws)==true)
return true;
else
return false;
}

You might also like