Showing posts with label iteration. Show all posts
Showing posts with label iteration. Show all posts

Binary Search in Java using Iteration and Recursion

Problem:

Create a binary search app in java using iteration and recursion.

Note: Binary search only works with sorted list of numbers.

Output:

Enter a target int
4
Target found iteratively!
Target found recursively!



Solution:

package binarySearchApp;

import java.util.Scanner;

public class BinarySearchApp {

 public static void main(String[] args) {
  int[] pool = {4,8,9,13,17,22};
  int target;
  Scanner scan = new Scanner(System.in);
  
  System.out.println("Enter a target int");
  target = scan.nextInt();
  boolean found;
  
//********************iterative version *************************   
  found = binarySearch_iter(pool, target);
  if (found) 
   System.out.println("Target found iteratively!");
  else
   System.out.println("Target not found iteratively!");
  
//********************recursive version ************************* 
  found = binarySearch_rec(pool, target, 0, pool.length-1);
  if (found) 
   System.out.println("Target found recursively!");
  else
   System.out.println("Target not found recursively!");
  
 }
 
//********************iterative method ************************* 
 private static boolean binarySearch_iter (int[] pool, int target) {
  
  int min = 0, max = pool.length-1, mid;
  boolean found = false;
  
  while (!found && min <= max) {
   mid = (min+max)/2;
   if (pool [mid] == target)
    found = true;
   else if (pool [mid] < target) 
    min = mid +1;
   else 
    max = mid -1;
   
  }
  return found;
 }

//********************recursive method ************************* 
 private static boolean binarySearch_rec (int[] pool, int target, int min, int max) {
  if (min > max)
   return false;
  else {
   int mid = (min + max)/2;
   if (pool[mid] == target)
    return true;
   else if (pool[mid] < target)
    return binarySearch_rec (pool, target, mid+1, max);
   else
    return binarySearch_rec (pool, target, min, mid-1);
  }
 }

}
Read More

Computer Fibonnaci Numbers with BigInteger in java

Problem:

Create a Fibonnaci program in java that calculate the fibbonaci numbers using BigInteger class. Include also iterative and recursive methods for the sake of comparaison.

Output:

Enter a positive int value (q to quit):
40
Iterative Fibonacci: 102334155
Recursion Fibonacci: 102334155
Duration: 2088
Accurate Fibonacci: 102334155
Enter a positive int value (q to quit):
q




Solution:

import java.math.BigInteger;
import java.util.Scanner;

public class FibonacciApp {

 public static void main(String[] args) {
  
  Scanner scan = new Scanner(System.in);
  int n;
  String n_str; 
  long fib_iter, fib_rec, time_in, time_out;
  BigInteger fib_acc;
  
  System.out.println("Enter a positive int value (q to quit):");
  n_str = scan.nextLine();
  
  while(!n_str.equalsIgnoreCase("q")) {
   try {
    n = Integer.parseInt(n_str);
    fib_iter = fibonacci_iter(n);
    System.out.println("Iterative Fibonacci: " + fib_iter);
    
    time_in = System.currentTimeMillis();
    
    fib_rec = fibonacci_rec(n);
    System.out.println("Recursion Fibonacci: " + fib_rec);
    
    time_out = System.currentTimeMillis();
    System.out.println("Duration: " + (time_out-time_in));
    
    fib_acc = fibonacci_acc(n);
    System.out.println("Accurate Fibonacci: " + fib_acc);
    
   } catch (NumberFormatException e) {
    System.out.println("Input must be numeric. Try Again");
   } 
   System.out.println("Enter a positive int value (q to quit):");
   n_str = scan.nextLine();
  }
  
  
 }
 
 private static long fibonacci_iter(int n) {
  
  if (n==0 || n==1) return n;
  else { 
   long previousprev = 0;
   long prev =1; 
   long current = 1L;
   
   for (int i=2 ; i <=n ; i++) {
   current = previousprev + prev; 
   previousprev = prev;
   prev = current;
   
   
   }
  return current;
  }
  
 }
 
 private static long fibonacci_rec(int n) {
  if (n ==0 || n==1 ) 
   return n;
  else 
   return fibonacci_rec(n-1)  + fibonacci_rec(n-2)
; }
 
 private static BigInteger fibonacci_acc(int n) {
  if (n==0 || n==1) 
   return BigInteger.valueOf(n);
  else {
   BigInteger prevprev = BigInteger.ZERO;
   BigInteger prev = BigInteger.ONE;
   BigInteger current = BigInteger.ONE;
   
   for (int i = 2; i <= n ; i++) {
    current = prevprev.add(prev);
    prevprev = prev;
    prev = current;
   }
   return current;
  }
 }
}
Read More

Finding the factorial of a number using iteration in Java

Problem:

Write a java method returns the factorial of a number using iteration.

Output:

Not applicable.

Solution:

public class interation_factorial
{
  static int factorial(int n )
  {
    int f = 1 ;
    for (int i =n;i>=1;i--)
    {
      f*=i;
    }
    return f;
  }
  public static void main(String[] args)
  {
    System.out.println(factorial(5));
  }
}
Read More

Finding the length of string using iteration in Java

Problem:

Write a java method returns the length of a string using iteration.

Output:

Not applicable.

Solution:

public class string_length_interation
{
  public static int length (String str)
  {
    char c = str.charAt(0);
    int count=0;
    while (c != '\0')
    {
      count++;
      c = str.charAt(count);
    }
    
    return count;
    
  }
  
  
  public static void main(String[] args)
  {
    System.out.println(length("Few"));
  }
}
Read More

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact