☕
NPTEL • Programming in Java • Programming Assignment

Study Glance Blog

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

JAVA • PROGRAMMING • WEEK 2

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

NPTEL • Programming in Java • Programming Assignment

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


Week 02 : Programming Assignment 1

Solution :


import java.util.Scanner;

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

        // Read length and width of the rectangle
        int length = sc.nextInt();
        int width = sc.nextInt();

        // Calculate area of the rectangle
        int area = length * width;

        // Output the area
        System.out.println("Area of the rectangle: " + area);
    }
}



Week 02 : Programming Assignment 2

Solution :


import java.util.Scanner;

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

        // Read length and width of the rectangle
        int length = sc.nextInt();
        int width = sc.nextInt();

        // Calculate the perimeter using the correct formula
        int perimeter = 2 * (length + width);

        // Output the perimeter
        System.out.println("Perimeter is: " + perimeter);

        sc.close();
    }
}




Week 02 : Programming Assignment 3

Solution :


import java.util.Scanner;

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

        int n = sc.nextInt();
        int[] arr = new int[n];

        // Read n numbers into array
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }

        int max = arr[0];  // Assume first element is maximum

        // Use a loop to find maximum element
        for (int i = 1; i < n; i++) {
            if (arr[i] > max) {
                max = arr[i];
            }
        }

        System.out.println("Maximum is: " + max);

        sc.close();
    }
}



Week 02 : Programming Assignment 4

Solution :


import java.util.Scanner;

public class W02_P4 {

    // Declare a class named Rectangle
    static class Rectangle {
        int length;
        int width;
    }

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

        // Read length and width
        int l = sc.nextInt();
        int w = sc.nextInt();

        // Create an object of the Rectangle class
        Rectangle rect = new Rectangle();

        // Assign values to the object's member variables
        rect.length = l;
        rect.width = w;

        // Print the sum using rect.length and rect.width
        System.out.println("Sum of length and width is: " + (rect.length + rect.width));

        sc.close();
    }
}




Week 02 : Programming Assignment 5

Solution :


import java.util.Scanner;

public class W02_P5 {

    // Declare a separate class named Circle
    static class Circle {
        int radius;

        // Constructor that takes radius as parameter
        Circle(int radius) {
            this.radius = radius;
        }
    }

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

        // Read radius value from user
        int r = sc.nextInt();

        // Create an object of Circle class using constructor
        Circle c = new Circle(r);

        // Print the radius using object member
        System.out.println("Radius of the circle is: " + c.radius);

        sc.close();
    }
}