Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.11 KB | None | 0 0
  1. import java.io.*;
  2.  
  3. public class Recursion
  4. {
  5.     public static void main( String[] args )
  6.     {
  7.  
  8.         // T R I S T A R S
  9.         int rows = 5;
  10.         System.out.format("A tringle with %d rows contains %d stars\n", rows, triStars( rows ) );
  11.        
  12.         // S U M D I G I T S
  13.         int number = 12345;
  14.         System.out.format("The sum of the digits in the number %d = %d\n", number, sumDigits( number ) );
  15.        
  16.         // C O U N T 7 S
  17.         number = 713274772;
  18.         System.out.format("There are %d occurances of the digit 7 in the number %d\n", count7s(number), number );
  19.        
  20.         // C O U N T 8 S
  21.         number = 82338828;
  22.         System.out.format("There are %d occurances of the digit 8 in the number %d\n", count8s(number), number );  
  23.  
  24.         // P O W E R N
  25.         int base=2,exponent=8;
  26.         System.out.format("%d to the power %d = %d\n", base, exponent, powerN(base,exponent) );
  27.    
  28.  
  29.         // I S S O R T E D
  30.         // perturb values as needed to test on an unsorted array
  31.         int[] array = { 7, 8, 12, 20, 21, 22, 37, 41, 55, 60, 65, 74, 83, 84, 87 };
  32.         int startingAt=0;
  33.         boolean isSorted = isSorted( array, startingAt, array.length );
  34.         System.out.print( "array: ");
  35.         for ( int i=0 ; i<array.length ; ++i ) System.out.print( array[i] + " " );
  36.         if (isSorted)
  37.             System.out.println(" is SORTED" ); 
  38.         else
  39.             System.out.println(" is NOT SORTED" ); 
  40.  
  41.  
  42.         // P A L I N D R O M E
  43.         String s = "stanleyyelnats"; // try with several differnt values that are or not palindromes   
  44.         if ( isPalindrome( s, 0, s.length()-1 ) )
  45.             System.out.format("%s IS a palindrome\n", s ); 
  46.         else
  47.             System.out.format("\n%s NOT a palindrome\n", s );  
  48.        
  49.        
  50.    
  51.     } // END MAIN
  52.  
  53.     // count stars in a triangle using # of rows as input
  54.     static int triStars(int rows)  
  55.     {   if (rows==0) return 0;
  56.         return rows + triStars(rows-1);
  57.     }
  58.     // given a number return the sum of the digits
  59.     static int sumDigits(int n)
  60.     {   if (n==0) return 0;
  61.         return (n%10) + sumDigits(n/10);
  62.     }
  63.     // given a number compute the number of 7's in that number
  64.     static int count7s(int n)
  65.     {   if (n==0) return 0;
  66.         if (n%10 ==7) return 1 + count7s(n/10);
  67.         else return count7s(n/10);
  68.     }
  69.     // given a number count the number of 8 but if an 8 has another 8 to its immdiate left count it as 2
  70.     // the number 8802388 will return a count of 6
  71.     static int count8s(int n)
  72.     {   if (n == 0) return 0;
  73.         if ( n % 10 == 8) {
  74.             int i = n/10;
  75.             if (i%10 == 8) {
  76.                 return 2 + count8s(n/10);
  77.             }
  78.         else{
  79.             return 1 + count8s(n/10);
  80.         }
  81.            
  82.     }      
  83.     }
  84.     //compute base to the power n
  85.     static int powerN(int base, int n)
  86.     {   if (n == 0) return 1;
  87.         return base * powerN(base, n-1);
  88.     }
  89.     // return true only if the array is sorted
  90.     static boolean isSorted(int array[], int i, int count )
  91.     {   boolean toReturn;
  92.         if (count == 1) return true;
  93.         if (array[i] <= array[i+1])
  94.         {
  95.             toReturn = isSorted(array, i+1, count-1);
  96.         }
  97.         else {
  98.             return false;
  99.         }
  100.         return toReturn;
  101.     }
  102.  
  103.     // return index of key or -1
  104.     static boolean isPalindrome(String s, int lo, int hi )
  105.     {   if(hi <= lo) return true;
  106.         else if (s.charAt(lo) != s.charAt(hi)) return false;
  107.         else return isPalindrome(s, lo + 1, hi - 1);       
  108.     }  
  109.    
  110. } // END CLASS Recursion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement