Back to the 2020 paper

Module III: Basics of Web Programming

202014m

Write a program in Java to do the following:

(a) To swap the two numbers without using the third variable
(b) Factorial of a number using recursion

Worked SolutionAI Assisted

Answer: Java Programs — Number Swapping & Recursive Factorial


Part (a): Swap Two Numbers Without Using a Third Variable

1. Algorithm & Logic

We can swap two numbers without a temporary variable using arithmetic addition/subtraction or bitwise XOR operations:

  • Step 1: a=a+ba = a + b (Holds sum of both)
  • Step 2: b=abb = a - b (Original value of aa)
  • Step 3: a=aba = a - b (Original value of bb)

2. Java Code:

import java.util.Scanner;

public class SwapWithoutThirdVariable {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        System.out.print("Enter first number (a): ");
        int a = sc.nextInt();
        System.out.print("Enter second number (b): ");
        int b = sc.nextInt();
        
        System.out.println("
--- Before Swapping ---");
        System.out.println("a = " + a + ", b = " + b);
        
        // Swapping logic using arithmetic operators
        a = a + b;
        b = a - b;
        a = a - b;
        
        System.out.println("
--- After Swapping ---");
        System.out.println("a = " + a + ", b = " + b);
        
        sc.close();
    }
}

3. Dry Run:

  • Initial: a=15,b=25a = 15, b = 25
  • Line 1 (a=a+ba = a + b): a=15+25=40a = 15 + 25 = 40
  • Line 2 (b=abb = a - b): b=4025=15b = 40 - 25 = 15 (bb now holds original aa)
  • Line 3 (a=aba = a - b): a=4015=25a = 40 - 15 = 25 (aa now holds original bb)
  • Result: a=25,b=15a = 25, b = 15 (Swapped successfully!)

Part (b): Factorial of a Number Using Recursion

1. Mathematical Principle & Base Case

Factorial(n)={1if n=0 or n=1(Base Case)n×Factorial(n1)if n>1(Recursive Step)\text{Factorial}(n) = \begin{cases} 1 & \text{if } n = 0 \text{ or } n = 1 \quad \text{(Base Case)} \\ n \times \text{Factorial}(n - 1) & \text{if } n > 1 \quad \text{(Recursive Step)} \end{cases}

2. Java Code:

import java.util.Scanner;

public class RecursiveFactorial {
    // Recursive function to calculate factorial
    public static long calculateFactorial(int n) {
        // Base Condition
        if (n <= 1) {
            return 1;
        }
        // Recursive Call
        return n * calculateFactorial(n - 1);
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        System.out.print("Enter a non-negative integer: ");
        int number = sc.nextInt();
        
        if (number < 0) {
            System.out.println("Factorial is not defined for negative numbers.");
        } else {
            long result = calculateFactorial(number);
            System.out.println("Factorial of " + number + " (" + number + "!) = " + result);
        }
        
        sc.close();
    }
}

3. Recursive Call Stack Trace (For n=4n = 4):

calculateFactorial(4)
  = 4 * calculateFactorial(3)
        = 3 * calculateFactorial(2)
              = 2 * calculateFactorial(1)
                    = 1 (Base case reached)
              = 2 * 1 = 2
        = 3 * 2 = 6
  = 4 * 6 = 24
Output: 24

Complete Unified Program

import java.util.Scanner;

public class CombinedExamDemo {
    public static void swap(int a, int b) {
        System.out.println("Before Swap: a = " + a + ", b = " + b);
        a = a ^ b; // Bitwise XOR swapping
        b = a ^ b;
        a = a ^ b;
        System.out.println("After Swap:  a = " + a + ", b = " + b);
    }

    public static long factorial(int n) {
        if (n <= 1) return 1;
        return n * factorial(n - 1);
    }

    public static void main(String[] args) {
        // Test (a)
        swap(10, 20);

        // Test (b)
        int num = 5;
        System.out.println("Factorial of " + num + " = " + factorial(num));
    }
}

Similar questions