[go: up one dir, main page]

0% found this document useful (0 votes)
40 views10 pages

Assignment No 8

The document contains 6 programming assignments related to hash tables in Java. The first assignment creates two hash tables ht1 and ht2 and inserts key-value pairs. It prints the mappings of each table. The second assignment adds elements to two hash tables ht1 and ht2 using the put() method and prints the mappings. The third assignment removes an element with key 4 from a hash table ht and prints the initial and updated mappings. The fourth assignment traverses a hash table ht using an enhanced for loop and prints the key-value pairs. The fifth assignment prints the Fibonacci series up to a given number N using a while loop. The sixth assignment calculates the Fibonacci
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views10 pages

Assignment No 8

The document contains 6 programming assignments related to hash tables in Java. The first assignment creates two hash tables ht1 and ht2 and inserts key-value pairs. It prints the mappings of each table. The second assignment adds elements to two hash tables ht1 and ht2 using the put() method and prints the mappings. The third assignment removes an element with key 4 from a hash table ht and prints the initial and updated mappings. The fourth assignment traverses a hash table ht using an enhanced for loop and prints the key-value pairs. The fifth assignment prints the Fibonacci series up to a given number N using a while loop. The sixth assignment calculates the Fibonacci
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

ASSIGNMENT NO. :- 08.

1. HASH TABLE PROBLEM.


PROGRAM

package com.company;
import java.io.*;
import java.util.*;

public class Main {

public static void main(String[] args) {


Hashtable<Integer, String> ht1 = new Hashtable<>();

Hashtable<Integer, String> ht2


= new Hashtable<Integer, String>();

ht1.put(1, "one");
ht1.put(2, "two");
ht1.put(3, "three");

ht2.put(4, "four");
ht2.put(5, "five");
ht2.put(6, "six");

// Print mappings to the console


System.out.println("Mappings of ht1 : " + ht1);
System.out.println("Mappings of ht2 : " + ht2);
}
}

OUTPUT :-
2. HASH TABLE PROBLEM ADDING ELEMENT.
PROGRAM

package com.company;
import java.io.*;
import java.util.*;

public class Main {


public static void main(String[] args) {

Hashtable<Integer, String> ht1 = new Hashtable<>();

Hashtable<Integer, String> ht2


= new Hashtable<Integer, String>();

// Inserting the Elements


// using put() method
ht1.put(1, "Geeks");
ht1.put(2, "For");
ht1.put(3, "Geeks");

ht2.put(1, "Geeks");
ht2.put(2, "For");
ht2.put(3, "Geeks");

// Print mappings to the console


System.out.println("Mappings of ht1 : " + ht1);
System.out.println("Mappings of ht2 : " + ht2);
}
}

OUTPUT
3. HASH TABLE PROBLEM REMOVING ELEMENT.
PROGRAM

package com.company;
import java.io.*;
import java.util.*;

public class Main {

public static void main(String[] args) {


// Initialization of a Hashtable
Map<Integer, String> ht
= new Hashtable<Integer, String>();

// Inserting the Elements


// using put method
ht.put(1, "Geeks");
ht.put(2, "For");
ht.put(3, "Geeks");
ht.put(4, "For");

// Initial HashMap
System.out.println("Initial map : " + ht);

// Remove the map entry with key 4


ht.remove(4);

// Final Hashtable
System.out.println("Updated map : " + ht);
}
}

OUTPUT

4. TRAVERSAL OF HASHTABLE
PROGRAM

package com.company;
import java.io.*;
import java.util.*;
public class Main {

public static void main(String[] args) {


// Create an instance of Hashtable
Hashtable<String, Integer> ht = new Hashtable<>();

// Adding elements using put method


ht.put("vishal", 10);
ht.put("sachin", 30);
ht.put("vaibhav", 20);

// Iterating using enhanced for loop


for (Map.Entry<String, Integer> e : ht.entrySet())
System.out.println(e.getKey() + " " + e.getValue());
}
}

OUTPUT
5. Different ways to print Fibonacci series in Java.

PROGRAM

package com.company;
import java.io.*;
import java.util.*;

public class Main {


static void Fibonacci(int N)
{
int num1 = 0, num2 = 1;

int counter = 0;

// Iterate till counter is N


while (counter < N) {

// Print the number


System.out.print(num1 + " ");

// Swap
int num3 = num2 + num1;
num1 = num2;
num2 = num3;
counter = counter + 1;
}
}
public static void main(String[] args) {
// Given Number N
int N = 10;

// Function Call
Fibonacci(N);
}
}

OUTPUT
6. Febbo. Using Hash
PROGRAM

package com.company;
import java.io.*;
import java.util.*;

public class Main {


public static int fibo(int n){
if(n==1)
return values[0];
if(n==2)
return values[1];
else{
values[n-1]=fibo(n-1)+fibo(n-2);
return values[n-1];
}
}
static int values[]=new int[1000];

public static void main(String[] args) {


int n;
Scanner sc= new Scanner(System.in);
n= sc.nextInt();
sc.close();
values[0]=0;
values[1]=1;
System.out.println(fibo(n));

}
}

OUTPUT

You might also like