Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class RecursiveMethods{
- public static void main(String[] args){
- System.out.println( isPalindrome("gohangasalamiimalasagnahog") );
- System.out.println( gcd(6807, 19) );
- System.out.println( countEvens( new int[]{1,2,3,4,5}, 0 ) );
- }
- public static int countEvens(int[] arr, int index){
- if( index == arr.length )
- return 0;
- else if( arr[index] % 2 == 0 )
- return 1 + countEvens(arr, index + 1);
- else
- return countEvens(arr, index + 1);
- }
- public static int gcd(int p, int q) {
- if (q == 0)
- return p;
- else
- return gcd(q, p % q);
- }
- public static boolean isPalindrome( String s ){
- if(s.length() <= 1)
- return true;
- else if(! s.substring(0,1).equals(s.substring(s.length()-1,s.length())))
- return false;
- else
- return isPalindrome(s.substring(1, s.length() - 1));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement