Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Progression
- {
- public static void main(String [] args)
- {
- Scanner keyboard = new Scanner (System.in);
- System.out.println(
- "This program will calculate the geometric and ");
- System.out.println(
- "harmonic progression for the number you enter.");
- System.out.print(
- "Enter an integer that is greater than or equal to 1: ");
- int input = keyboard.nextInt();
- int geomAnswer = geometricRecursive (input);
- double harmAnswer = harmonicRecursive (input);
- System.out.println("Using recursion:");
- System.out.println("The geometric progression of " + input + " is " + geomAnswer + "\n");
- System.out.println("The harmonic progression of " + input + " is " + harmAnswer + "\n");
- geomAnswer = geometricIterative (input);
- harmAnswer = harmonicIterative (input);
- System.out.println("Using iteration:");
- System.out.println("The geometric progression of " + input + " is " + geomAnswer + "\n");
- System.out.println("The harmonic progression of " + input + " is " + harmAnswer + "\n");
- } // end main method
- static int geometricIterative(int baseCase){
- int result = 1;
- for(int i = 1; i < baseCase; i++){
- int tempMulti;
- tempMulti = i + 1;
- result =+ result * tempMulti;
- // remove comment // to visually check the results case by case
- System.out.println(i + " * " + tempMulti + " = " + result);
- } // end for loop
- return result;
- } // end geometricIterative() method
- static double harmonicIterative(int baseCase){
- double result = 1;
- for(int i = 1; i < baseCase; i++){
- double tempDiv;
- tempDiv = i + 1;
- result =+ result / tempDiv;
- // remove comment // to visually check the results case by case
- System.out.println(i + " / " + (int)tempDiv + " = " + result);
- } // end for loop
- return result;
- } // end harmonicIterative() method
- // Variables needed for the Geometric / Harmonic Recursive
- // method
- // Cycle Keeps track of how many times recursive calls there are.
- static int cycles = 1;
- // I or D _Result are used for storage of the recursive results
- static int I_Result = 1;
- static double D_Result = 1;
- static int I_Temp_Result = 0;
- static double D_Temp_Result = 0;
- static int geometricRecursive(int baseCase){
- if(baseCase > cycles){
- int tempMulti;
- tempMulti = cycles + 1;
- I_Result =+ I_Result * tempMulti;
- // remove comment // to visually check the results case by case
- System.out.println(cycles + " * " + tempMulti + " = " + I_Result);
- cycles++;
- geometricRecursive(baseCase);
- } // end if
- else if (baseCase == cycles){
- I_Temp_Result = I_Result;
- return I_Temp_Result;
- } // end else if
- cycles = 1;
- I_Result = 1;
- return I_Temp_Result;
- } // end geometricRecursive() method
- static double harmonicRecursive(int baseCase){
- if(baseCase > cycles){
- double tempDiv;
- tempDiv = cycles + 1;
- D_Result =+ D_Result / tempDiv;
- // remove comment // to visually check the results case by case
- System.out.println(cycles + " / " + (int)tempDiv + " = " + D_Result);
- cycles++;
- harmonicRecursive(baseCase);
- } // end if
- // figure out how to return the proper value for both of the recursive methods
- else if (baseCase == cycles){
- D_Temp_Result = D_Result;
- return D_Temp_Result;
- } // end else if
- cycles = 1;
- D_Result = 1;
- return D_Temp_Result;
- } // end geometricRecursive() method
- } // end Progression class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement