Home

Swayam Solver

Learn Programming & Prepare for NPTEL Exams...

Find your course

Explore Courses

Latest Blog Posts

NPTEL Programming In Java Programming Assignment July-2026 Swayam


Programming In Java - July 2026

NPTEL

 Please scroll down for latest Programs   ðŸ‘‡ 


Code compiled and tested successfully!


W03 Programming Assignments 1

Due date on 2026-08-14, 23:59 IST

Your last recorded submission was on 2026-08-13, 11:43 IST.

Q.

Write a program to print the factorial of a number by defining a recursive method named 'Factorial'.
Factorial of any number n is represented by n! and is equal to 1*2*3*....*(n-1)*n. E.g.-
4! = 1*2*3*4 = 24
3! = 3*2*1 = 6
2! = 2*1 = 2
Also,
1! = 1
0! = 1
(Remember to match the output given exactly, including the spaces and new lines)
(passed with presentation error means you will get full marks)

Complete Program

Java
import java.util.Scanner;

class W03_P1 {

    // --- START OF SOLUTION CODE ---
    public static int factorial(int x) {
        if (x == 0 || x == 1) {
            return 1;
        } else {
            return factorial(x - 1) * x;
        }
    }
    // --- END OF SOLUTION CODE ---

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int x;
        x = in.nextInt();
        System.out.println(factorial(x));
    }
}

Code Snippet to Paste in the Editor

Java
    // --- START OF SOLUTION CODE ---
    public static int factorial(int x) {
        if (x == 0 || x == 1) {
            return 1;
        } else {
            return factorial(x - 1) * x;
        }
    }
    // --- END OF SOLUTION CODE ---


You may submit any number of times before the due date. The final submission will be considered for grading.

This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.

Evaluation Results

CompilationSuccessful|Public Test Cases: 1/1 Passed

Note: These tests may not be considered while scoring.

Public Test Cases

Test Case
Input
Expected Output
Output
Status
Test Case 1

5

120
120\n
Presentation Error


W03 Programming Assignments 2

Due date on 2026-08-14, 23:59 IST

Q.

There are two class cls1 and cls2 which is subclass of cls1.  cls1 having a method "add" which add two numbers.
Create two method inside cls2 which will take 2 parameters as input i.e. a and b and print the sum , multiplication and sum of their squares i.e (a^2) + (b^2).
(Remember to match the output given exactly, including the spaces and new lines)
(passed with presentation error means you will get full marks)

Complete Program

Java
import java.util.Scanner;

class cls1
{
    void add(int p,int q)
    {
        System.out.println(p+q);
    }
}

// --- START OF SOLUTION CODE ---
class cls2 extends cls1
{
    void mul(int p, int q)
    {
        System.out.println(p * q);
    }
    void task(int p, int q)
    {
        System.out.println((p * p) + (q * q));
    }
}
// --- END OF SOLUTION CODE ---

public class W03_P2{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        cls2 obj=new cls2();
        int a=sc.nextInt();
        int b=sc.nextInt();
        //String tilde=sc.next();
        obj.add(a,b);
        obj.mul(a,b);
        obj.task(a,b);
    }
}

Code Snippet to Paste in the Editor

Java
// --- START OF SOLUTION CODE ---
class cls2 extends cls1
{
    void mul(int p, int q)
    {
        System.out.println(p * q);
    }
    void task(int p, int q)
    {
        System.out.println((p * p) + (q * q));
    }
}
// --- END OF SOLUTION CODE ---
You may submit any number of times before the due date. The final submission will be considered for grading.

This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.

Evaluation Results

CompilationSuccessful|Public Test Cases: 1/1 Passed

Note: These tests may not be considered while scoring.

Public Test Cases

Test Case
Input
Expected Output
Output
Status
Test Case 1

2 4

6\n
8\n
20
6\n
8\n
20\n
Presentation Error


W03 Programming Assignments 3

Due date on 2026-08-14, 23:59 IST

Q.

Complete the code segment to count number of digits in an integer using while loop.

Complete Program

Java
import java.util.Scanner;

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

        // --- START OF SOLUTION CODE ---
        int count = 0;
        while (num != 0) {
            num /= 10;
            ++count;
        }
        System.out.print(count);
        // --- END OF SOLUTION CODE ---
    }
}

Code Snippet to Paste in the Editor

Java
// --- START OF SOLUTION CODE ---
        int count = 0;
        while (num != 0) {
            num /= 10;
            ++count;
        }
        System.out.print(count);
// --- END OF SOLUTION CODE ---


You may submit any number of times before the due date. The final submission will be considered for grading.

This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.

Evaluation Results

CompilationSuccessful|Public Test Cases: 2/2 Passed

Note: These tests may not be considered while scoring.

Public Test Cases

Test Case
Input
Expected Output
Output
Status
Test Case 1

153

3
3
-
Test Case 2

0003452

4
4
-


W03 Programming Assignments 4

Due date on 2026-08-14, 23:59 IST

Q.

A Student class with private fields (name, age) is provided,
Your task is to make the following:
  • a parameterized constructor to initialize the private fields
  • the getter/setter methods for each field
Follow the naming convention as given in the main method of the suffix code.
(Use Student.java as file name here)

Complete Program

Java
import java.util.Scanner;

class Student {
    private String name;
    private int age;

    // --- START OF SOLUTION CODE ---
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    // --- END OF SOLUTION CODE ---

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // System.out.print("Enter student name: ");
        String name = scanner.next();
        // System.out.print("Enter student age: ");
        int age = scanner.nextInt();
        Student student = new Student(name, age);
        System.out.print("Name: " + student.getName() + ", Age: " + student.getAge());
        scanner.close();
    }
}

Code Snippet to Paste in the Editor

Java
    // --- START OF SOLUTION CODE ---
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    // --- END OF SOLUTION CODE ---


You may submit any number of times before the due date. The final submission will be considered for grading.

This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.

Evaluation Results

CompilationSuccessful|Public Test Cases: 2/2 Passed

Note: These tests may not be considered while scoring.

Public Test Cases

Test Case
Input
Expected Output
Output
Status
Test Case 1

John 20

Name: John, Age: 20
Name: John, Age: 20
-
Test Case 2

Alice 25

Name: Alice, Age: 25
Name: Alice, Age: 25
-



W03 Programming Assignments 5

Due date on 2026-08-14, 23:59 IST

Q.

Complete the code segment to display the factors of a number n.

Complete Program

Java
import java.util.Scanner;

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

        // --- START OF SOLUTION CODE ---
        for (int i = 1; i <= num; i++) {
            if (num % i == 0) {
                System.out.print(i + " ");
            }
        }
        // --- END OF SOLUTION CODE ---
    }
}

Code Snippet to Paste in the Editor

Java
        // --- START OF SOLUTION CODE ---
        for (int i = 1; i <= num; i++) {
            if (num % i == 0) {
                System.out.print(i + " ");
            }
        }
        // --- END OF SOLUTION CODE ---


You may submit any number of times before the due date. The final submission will be considered for grading.

This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.

Evaluation Results

CompilationSuccessful|Public Test Cases: 1/1 Passed

Note: These tests may not be considered while scoring.

Public Test Cases

Test Case
Input
Expected Output
Output
Status
Test Case 1

18

1 2 3 6 9 18
1 2 3 6 9 18 
Presentation Error