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=[Link]();
String nws=""; 5. Method to reverse a string
for(int i=n-1;i>=0;--i){
nws=nws+[Link](i);
}
return nws ;
}
static boolean palin(String s){
int n=[Link]();
String nws="";
for(int i=n-1;i>=0;--i){ 6. Method to find whether a given String is ‘Palindrome’ or
nws=nws+[Link](i); Not, Palindrome string is a string which reads the same
} from left and Right. Ex. ‘dad’, ‘mom’
if([Link](nws)==true)
return true;
else
return false;
}