[go: up one dir, main page]

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

Coding Set 2

Uploaded by

vernonhosting
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)
14 views10 pages

Coding Set 2

Uploaded by

vernonhosting
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

Question Paper Set 2

1. Factorial Calculation

 Problem: Write a program that computes the factorial of a given non-negative integer n.

 Sample Input:
Input: 4

 Sample Output:
Output: 24
(Explanation: 4! = 4 * 3 * 2 * 1 = 24)

2. Find Prime Numbers

 Problem: Create a function that returns a list of all prime numbers up to a given number n.

 Sample Input:
Input: 10

 Sample Output:
Output: [2, 3, 5, 7]

3. Anagram Checker

 Problem: Write a function that checks if two given strings are anagrams of each other.

 Sample Input:
Input: "listen", "silent"

 Sample Output:
Output: True

4. Matrix Multiplication

 Problem: Given two matrices, write a program to multiply them.

 Sample Input:
Input: [[1, 2], [3, 4]] and [[5, 6], [7, 8]]

 Sample Output:
Output: [[19, 22], [43, 50]]

5. Implement a Queue Using Stacks

 Problem: Write a program to implement a queue using two stacks.


 Sample Input:
Operations: enqueue(1), enqueue(2), enqueue(3), dequeue()

 Sample Output:
Output: 1
(Explanation: The first element enqueued, 1, is dequeued first.)
Answers :

1. Factorial Calculation

Python:

def factorial(n):

if n == 0:

return 1

result = 1

for i in range(2, n+1):

result *= i

return result

print(factorial(4))

# Output: 24

JAVA :

public class Factorial {

public static int factorial(int n) {

int result = 1;

for (int i = 2; i <= n; i++) {

result *= i;

return result;

}
public static void main(String[] args) {

System.out.println(factorial(4));

// Output: 24

2. Find Prime Numbers

Python:

def is_prime(n):

if n <= 1:

return False

for i in range(2, int(n ** 0.5) + 1):

if n % i == 0:

return False

return True

def find_primes(n):

return [x for x in range(2, n+1) if is_prime(x)]

print(find_primes(10))

# Output: [2, 3, 5, 7]

JAVA :

import java.util.ArrayList;
import java.util.List;
public class PrimeNumbers {
public static boolean isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
}

public static List<Integer> findPrimes(int n) {


List<Integer> primes = new ArrayList<>();
for (int i = 2; i <= n; i++) {
if (isPrime(i)) {
primes.add(i);
}
}
return primes;
}

public static void main(String[] args) {


System.out.println(findPrimes(10));
// Output: [2, 3, 5, 7]
}
}

3. Anagram Checker

Python:
def are_anagrams(str1, str2):

return sorted(str1) == sorted(str2)

print(are_anagrams("listen", "silent"))

# Output: True

JAVA :

import java.util.Arrays;

public class AnagramChecker {

public static boolean areAnagrams(String str1, String str2) {

char[] arr1 = str1.toCharArray();

char[] arr2 = str2.toCharArray();

Arrays.sort(arr1);

Arrays.sort(arr2);

return Arrays.equals(arr1, arr2);

public static void main(String[] args) {

System.out.println(areAnagrams("listen", "silent"));

// Output: true

4. Matrix Multiplication

Python:
def matrix_multiply(A, B):

result = [[0] * len(B[0]) for _ in range(len(A))]

for i in range(len(A)):

for j in range(len(B[0])):

for k in range(len(B)):

result[i][j] += A[i][k] * B[k][j]

return result

A = [[1, 2], [3, 4]]

B = [[5, 6], [7, 8]]

print(matrix_multiply(A, B))

# Output: [[19, 22], [43, 50]]

JAVA :

import java.util.Arrays;

public class MatrixMultiplication {

public static int[][] multiplyMatrices(int[][] A, int[][] B) {

int rowsA = A.length;

int colsA = A[0].length;

int colsB = B[0].length;

int[][] result = new int[rowsA][colsB];

for (int i = 0; i < rowsA; i++) {

for (int j = 0; j < colsB; j++) {

for (int k = 0; k < colsA; k++) {

result[i][j] += A[i][k] * B[k][j];

}
}

return result;

public static void main(String[] args) {

int[][] A = {{1, 2}, {3, 4}};

int[][] B = {{5, 6}, {7, 8}};

int[][] result = multiplyMatrices(A, B);

System.out.println(Arrays.deepToString(result));

// Output: [[19, 22], [43, 50]]

5. Implement a Queue Using Stacks

Python:

class QueueUsingStacks:

def __init__(self):

self.stack1 = []

self.stack2 = []

def enqueue(self, item):

self.stack1.append(item)

def dequeue(self):

if not self.stack2:

while self.stack1:

self.stack2.append(self.stack1.pop())

return self.stack2.pop() if self.stack2 else None


queue = QueueUsingStacks()

queue.enqueue(1)

queue.enqueue(2)

queue.enqueue(3)

print(queue.dequeue())

# Output: 1

JAVA :

import java.util.Stack;

public class QueueUsingStacks {

private Stack<Integer> stack1 = new Stack<>();

private Stack<Integer> stack2 = new Stack<>();

public void enqueue(int item) {

stack1.push(item);

public int dequeue() {

if (stack2.isEmpty()) {

while (!stack1.isEmpty()) {

stack2.push(stack1.pop());

return stack2.isEmpty() ? -1 : stack2.pop();

}
public static void main(String[] args) {

QueueUsingStacks queue = new QueueUsingStacks();

queue.enqueue(1);

queue.enqueue(2);

queue.enqueue(3);

System.out.println(queue.dequeue());

// Output: 1

You might also like