Menu


NPTEL - Programming in Java - Week 06 : Programming Assignments Answers- 2025




Programming in Java
NPTEL Week 06 : Programming Assignments Answers- 2025

Week 06 : Programming Assignment 6 - Programming In Java

Week 06 : Programming Assignments - NPTEL >> Programming In Java - 2025


Week 06 : Programming Assignment 1

Solution :


import java.util.Scanner;

public class W06_P1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        // Read two integers
        int num1 = sc.nextInt();
        int num2 = sc.nextInt();

        // Use try-catch to handle possible run-time error
        try {
		

		// Answer
		System.out.println("Result is: " + (num1 / num2));
		//End
		

	} catch (ArithmeticException e) {
            // Print safe message if division by zero occurs
            System.out.println("Cannot divide by zero");
        }

        sc.close();
    }
}



Week 06 : Programming Assignment 2

Solution :


import java.util.Scanner;

public class W06_P2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        // Read two integers
        int num1 = sc.nextInt();
        int num2 = sc.nextInt();

        // Outer try-catch block
        try {

            // Inner try-catch block for division operation
            try {

		// Answer
		int result = num1 / num2;
		System.out.println("Division successful");
		System.out.println("Result is: " + result);

		//End
		

		} catch (ArithmeticException e) {
                System.out.println("Cannot divide by zero");
            }

        } catch (Exception e) {
            // Handles other unexpected errors
            System.out.println("An unexpected error occurred");
        }

        sc.close();
    }
}



Week 06 : Programming Assignment 3

Solution :


import java.util.Scanner;

public class W06_P3 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        // Read two integers
        int num1 = sc.nextInt();
        int num2 = sc.nextInt();

        // Try block with multiple catch blocks
        try {

		// Answer
		int result = num1 / num2;
		System.out.println("Division successful");
		System.out.println("Result is: " + result);
			// End
		

	   } catch (ArithmeticException e) {
            // Handles division by zero error
            System.out.println("Cannot divide by zero");
        } catch (Exception e) {
            // Handles other general errors
            System.out.println("An unexpected error occurred");
        }

        sc.close();
    }
}


Week 06 : Programming Assignment 4

Solution :


import java.util.Scanner;

public class W06_P4 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        // Read two integers
        int num1 = sc.nextInt();
        int num2 = sc.nextInt();

        // try-catch-finally structure
        try {

		// ANSWER
		int result = num1 / num2;
		System.out.println("Result is: " + result);
		// END

	} catch (ArithmeticException e) {
            System.out.println("Cannot divide by zero");
        } finally {
            // Print final message, runs always
            System.out.println("Program Ended");
        }

        sc.close();
    }
}


Week 06 : Programming Assignment 5

Solution :


import java.util.Scanner;

public class W06_P5 {

    // Method to calculate square root, may throw Exception
    public static double calculateSquareRoot(double num) throws Exception {
		

	//ANSWER
	if (num < 0) {
            throw new Exception("Cannot calculate square root of a negative number.");
        }

        return Math.sqrt(num);
		//END
		

	}

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        double number = sc.nextDouble();

        try {
            double result = calculateSquareRoot(number);
            System.out.println("Square root is: " + result);
        } catch (Exception e) {
            System.out.println("Cannot calculate square root of negative number");
        }

        sc.close();
    }
}


Relevant blogs :

NPTEL - Programming in Java - QUIZ : Week 12:Assignment 12 Answers- 2025

NPTEL - Programming in Java - Week 12 : Programming Assignments Answers- 2025

NPTEL - Programming in Java - QUIZ : Week 11:Assignment 11 Answers- 2025

NPTEL - Programming in Java - Week 11 : Programming Assignments Answers- 2025

NPTEL - Programming in Java - QUIZ : Week 10:Assignment 10 Answers- 2025

NPTEL - Programming in Java - Week 10 : Programming Assignments Answers- 2025

NPTEL - Programming in Java - QUIZ : Week 9:Assignment 9 Answers- 2025

NPTEL - Programming in Java - Week 09 : Programming Assignments Answers- 2025

NPTEL - Programming in Java - QUIZ : Week 8:Assignment 8 Answers- 2025

NPTEL - Programming in Java - Week 08 : Programming Assignments Answers- 2025

NPTEL - Programming in Java - QUIZ : Week 7:Assignment 7 Answers- 2025

NPTEL - Programming in Java - Week 07 : Programming Assignments Answers- 2025

NPTEL - Programming in Java - QUIZ : Week 6:Assignment 6 Answers- 2025

NPTEL - Programming in Java - Week 06 : Programming Assignments Answers- 2025

NPTEL - Programming in Java - QUIZ : Week 5:Assignment 5 Answers- 2025

NPTEL - Programming in Java - Week 05 : Programming Assignments Answers- 2025

NPTEL - Programming in Java - QUIZ : Week 4:Assignment 4 Answers- 2025

NPTEL - Programming in Java - Week 04 : Programming Assignments Answers- 2025

NPTEL - Programming in Java - QUIZ : Week 3:Assignment 3 Answers- 2025

NPTEL - Programming in Java - Week 03 : Programming Assignments Answers- 2025

NPTEL - Programming in Java - QUIZ : Week 2:Assignment 2 Answers- 2025

NPTEL - Programming in Java - Week 02 : Programming Assignments Answers- 2025

NPTEL - Programming in Java - QUIZ : Week 1:Assignment 1 Answers- 2025

NPTEL - Programming in Java - Week 01 : Programming Assignments Answers- 2025



Other blogs :

NPTEL - Introduction to Programming in C - Week 7:Assignment 7 Answers- 2025

NPTEL - Introduction to Programming in C - Week 6:Assignment 6 Answers- 2025

NPTEL - Introduction to Programming in C - Week 5:Assignment 5 Answers- 2025

NPTEL - Introduction to Programming in C - Week 4:Assignment 4 Answers- 2025

NPTEL - Introduction to Programming in C - Week 3:Assignment 3 Answers- 2025

NPTEL - Introduction to Programming in C - Week 2:Assignment 2 Answers- 2025

NPTEL - Introduction to Programming in C - Week 1:Assignment 1 Answers- 2025