Home

Learn Programming & Prepare for NPTEL Exams... Swayam Solver is your one-stop destination for NPTEL exam preparation.

NPTEL Programming in Java Jan 2024 Week 6 to 12















  Please scroll down for latest Programs. ðŸ‘‡ 



Week 6 : Programming Assignment 1

Due on 2024-03-14, 23:59 IST
Complete the code fragment to read two integer inputs from keyboard and find the quotient and remainder.
Your last recorded submission was on 2024-03-01, 22:30 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
public class Question61{
3
       public static void main(String[] args) {
4
       Scanner sc = new Scanner(System.in);
5
       int x=sc.nextInt();
6
       int y=sc.nextInt();
7
//code for quotient and remainder
8
        int Quotient = x/y ;
9
        int Remainder = x % y ;
10
11
        System.out.println("The Quotient is = " + Quotient );
12
        System.out.print("The Remainder is = " + Remainder );
0
}
1
}
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.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
556
9
The Quotient is = 61\n
The Remainder is = 7
The Quotient is = 61\n
The Remainder is = 7
Passed






Week 6 : Programming Assignment 2

Due on 2024-03-14, 23:59 IST
Complete the code segment to count number of digits in an integer using while loop.
Your last recorded submission was on 2024-03-01, 22:32 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
public class Question62{
3
    public static void main(String[] args) {
4
       Scanner sc = new Scanner(System.in);
5
       int num=sc.nextInt();
6
//Use while loop to count number of digits in an integer
7
int count = 0;
8
    while ( num !=0)
9
    {
10
num= num/10;
11
count++;
12
    }
13
14
System.out.print(count);
15
0
}
1
}
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.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
153
3
3
Passed
Test Case 2
0003452
4
4
Passed






Week 6 : Programming Assignment 3

Due on 2024-03-14, 23:59 IST
Complete the code segment to display the factors of a number n.
Your last recorded submission was on 2024-03-01, 22:37 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
public class Question63{
3
    public static void main(String[] args) {
4
       Scanner sc = new Scanner(System.in);
5
       int num =sc.nextInt();
6
//Use while or for loop to find the factors of a number.
7
int i = 1;
8
while ( i < num ){
9
  if ( num % i == 0 )
10
    System.out.print(i + " ");
11
  i++;
12
}
13
System.out.print(num);
0
}
1
}
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.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
18
1 2 3 6 9 18
1 2 3 6 9 18
Passed





Week 6 : Programming Assignment 4

Due on 2024-03-14, 23:59 IST
Complete the code segment to find the standard deviation of the 1-D array.
Use the following formula:
=1=1()2
Here,
σ = Population standard deviation
N = Number of observations in the population
Xi = ith observation in the population
μ = Population mean

Your last recorded submission was on 2024-03-01, 23:03 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
public class Question64{     
3
  
4
    public static void main(String[] args) 
5
    {
6
        double sum = 0.0;
7
        double standardDeviation = 0.0;
8
        double mean = 0.0;
9
        double res = 0.0;
10
        double sq = 0.0;
11
        Scanner sc = new Scanner(System.in);
12
        int num = sc.nextInt();
13
        double arr[] = new double[num];
14
        for (int i = 0; i < num; i++) {
15
            arr[i] = sc.nextDouble();
16
        }
17
// write code to find standard deviation of input array
18
for (int i = 0; i < num; i++) {
19
            sum += arr[i];
20
        }
21
mean = sum/num;
22
for (int i = 0; i < num; i++) {
23
            sq += (arr[i]-mean)*(arr[i]-mean);
24
        }
25
standardDeviation = sq/num;
26
res = Math.sqrt(standardDeviation);
0
System.out.print("Standard Deviation: " + res);
1
  }
2
}
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.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
10
1 2 3 4 5 6 7 8 9 10
Standard Deviation: 2.8722813232690143
Standard Deviation: 2.8722813232690143
Passed





Week 6 : Programming Assignment 5

Due on 2024-03-14, 23:59 IST
Write a program which will print a pattern of "*" 's of height "n".
For example:
Input:
n = 3
Output:
***
**
*
**
***
Your last recorded submission was on 2024-03-01, 22:51 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.*;
2
public class Question65{
3
    public static void main(String[] args) {
4
        Scanner inr = new Scanner(System.in);
5
       int n = inr.nextInt();
6
// Add the necessary code in the below space
7
for ( int i = 0 ; i < n ; i++){
8
  for (int j = n-i-1  ; j >=0 ; j--) {
9
    System.out.print("*");
10
  }
11
  System.out.print("\n");
12
}
13
for ( int i = 1 ; i < n ; i++){
14
  for (int j = 0  ; j <= i ; j++) {
15
    System.out.print("*");
16
  }
17
  System.out.print("\n");
18
}
0
}
1
}
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.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
6
******\n
*****\n
****\n
***\n
**\n
*\n
**\n
***\n
****\n
*****\n
******
******\n
*****\n
****\n
***\n
**\n
*\n
**\n
***\n
****\n
*****\n
******\n
Passed after ignoring Presentation Error












Week 7 : Programming Assignment 1

Due on 2024-03-14, 23:59 IST
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.
Your last recorded submission was on 2024-03-03, 11:41 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
class Student {
3
    private String name;
4
    private int age;
5
// ================================
6
    // TODO: Implement the Student class with private fields (name, age),
7
    //       a constructor, and getter/setter methods.
8
Student(String n, int a) {
9
   name = n;
10
   age = a;
11
}
12
public String getName() {
13
    return name;
14
  }
15
public int getAge() {
16
    return age;
17
  }
0
public static void main(String[] args) {
1
        Scanner scanner = new Scanner(System.in);
2
3
        // System.out.print("Enter student name: ");
4
        String name = scanner.next();
5
6
        // System.out.print("Enter student age: ");
7
        int age = scanner.nextInt();
8
9
        Student student = new Student(name, age);
10
11
        System.out.print("Name: " + student.getName() + ", Age: " + student.getAge());
12
13
        scanner.close();
14
    }
15
}
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.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
John
20
Name: John, Age: 20
Name: John, Age: 20
Passed
Test Case 2
Alice
25
Name: Alice, Age: 25
Name: Alice, Age: 25
Passed





Week 7 : Programming Assignment 2

Due on 2024-03-14, 23:59 IST
A BankAccount class with private field balance is provided,
Your task is to make the following: 
  • a parameterized constructor to initialize the private field
  • public void deposit(...) // to deposit money
  • public void withdraw(...) // to withdraw money, should print "Insufficient funds!" if not enough money to withdraw
  • public double getBalance() // to return the current balance
Follow the naming convention as given in the main method of the suffix code.
Your last recorded submission was on 2024-03-03, 11:57 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
3
class BankAccount {
4
    private double balance;
5
// ================================
6
    // TODO: Implement a BankAccount class with a private balance field,
7
    //       methods to deposit and withdraw, and a method to get the balance.
8
BankAccount(double initialBalance){
9
  balance = initialBalance;
10
}
11
12
public void deposit(double dAmount) {
13
    this.balance += dAmount;
14
  }
15
public void withdraw(double wAmount) {
16
    if(this.balance >= wAmount)
17
        this.balance -= wAmount;
18
    else
19
      System.out.println("Insufficient funds!");
20
  }
21
public double getBalance(){
22
    return(this.balance);
23
  }
24
0
public static void main(String[] args) {
1
        Scanner scanner = new Scanner(System.in);
2
3
        // System.out.print("Enter initial balance: ");
4
        double initialBalance = scanner.nextDouble();
5
6
        BankAccount account = new BankAccount(initialBalance);
7
8
        // System.out.print("Enter deposit amount: ");
9
        double depositAmount = scanner.nextDouble();
10
        account.deposit(depositAmount);
11
12
        // System.out.print("Enter withdrawal amount: ");
13
        double withdrawalAmount = scanner.nextDouble();
14
        account.withdraw(withdrawalAmount);
15
16
        System.out.print("Balance: " + account.getBalance());
17
18
        scanner.close();
19
    }
20
}
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.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
500
300
200
Balance: 600.0
Balance: 600.0
Passed
Test Case 2
500
100
900
Insufficient funds!\n
Balance: 600.0
Insufficient funds!\n
Balance: 600.0
Passed





Week 7 : Programming Assignment 3

Due on 2024-03-14, 23:59 IST
An abstract class shape is provided,
Your task is to make the following: 
  • a parameterized constructor to initialize the Circle class
  • @Override the area function to compute the area of a circle (Use Math.PI for value of pi, not 22/7)
  • @Override the displayInfo() function to print exactly in the format provided by the test cases.
Follow the naming convention as given in the main method of the suffix code.
Your last recorded submission was on 2024-03-03, 12:02 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
3
abstract class Shape {
4
    public abstract double area();
5
6
    public void displayInfo() {
7
        System.out.println("Shape - Area: " + area());
8
    }
9
}
10
11
final class Circle extends Shape {
12
    private double radius;
13
// ================================
14
    // TODO: write the contructor, area() and displayInfo() definitions here
15
Circle(double radius){
16
  this.radius = radius;
17
}
18
public double area(){
19
  return( Math.PI * radius * radius);
20
}
21
public void displayInfo() {
22
        System.out.print("Circle - Radius: " + this.radius + ", Area: " + area());
23
    }
0
public static void main(String[] args) {
1
        Scanner scanner = new Scanner(System.in);
2
3
        // System.out.print("Enter the radius of the circle: ");
4
        double radius = scanner.nextDouble();
5
6
        Circle circle = new Circle(radius);
7
        circle.displayInfo();
8
9
        scanner.close();
10
    }
11
}
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.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
5.0
Circle - Radius: 5.0, Area: 78.53981633974483
Circle - Radius: 5.0, Area: 78.53981633974483
Passed
Test Case 2
7.5
Circle - Radius: 7.5, Area: 176.71458676442586
Circle - Radius: 7.5, Area: 176.71458676442586
Passed






Week 7 : Programming Assignment 4

Due on 2024-03-14, 23:59 IST
An interface shape is provided,
Your task is to make the following: 
  • Create a class "Circle" that implements the "Shape" interface and provides its own implementation for calculateArea().
  • @Override the area function to compute the area of a circle (Use Math.PI for value of pi, not 22/7)
  • Create another class "Rectangle" that implements the "Shape" interface and provides its own implementation for calculateArea().
  • @Override the area function to compute the area of a rectangle
Follow the naming convention as given in the main method of the suffix code.
Your last recorded submission was on 2024-03-03, 12:25 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
3
interface Shape {
4
    double calculateArea();
5
}
6
// TODO: Create a class "Circle" that implements the "Shape" interface and provides its own implementation for calculateArea().
7
class Circle implements Shape{
8
    private double radius;
9
    Circle(double radius){
10
      this.radius = radius;
11
    }
12
    public double calculateArea() {
13
      return( Math.PI * radius * radius);
14
    }
15
}
16
// TODO: Create another class "Rectangle" that implements the "Shape" interface and provides its own implementation for calculateArea().
17
class Rectangle implements Shape{
18
    private double length;
19
    private double width;
20
    Rectangle(double length, double width){
21
      this.length = length;
22
      this.width = width;
23
    }
24
    public double calculateArea() {
25
      return(length * width);
26
    }
27
}
0
class NPTEL{
1
    public static void main(String[] args) {
2
        Scanner scanner = new Scanner(System.in);
3
4
        // Test the Circle class
5
        // System.out.print("Enter the radius of the circle: ");
6
        double circleRadius = scanner.nextDouble();
7
        Circle circle = new Circle(circleRadius);
8
        System.out.println("Circle Area: " + circle.calculateArea());
9
10
        // Test the Rectangle class
11
        // System.out.print("Enter the length of the rectangle: ");
12
        double rectangleLength = scanner.nextDouble();
13
        // System.out.print("Enter the width of the rectangle: ");
14
        double rectangleWidth = scanner.nextDouble();
15
        Rectangle rectangle = new Rectangle(rectangleLength, rectangleWidth);
16
        System.out.print("Rectangle Area: " + rectangle.calculateArea());
17
18
        scanner.close();
19
    }
20
}
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.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
5.0
2.5 5.0
Circle Area: 78.53981633974483\n
Rectangle Area: 12.5
Circle Area: 78.53981633974483\n
Rectangle Area: 12.5
Passed
Test Case 2
0.0
0.0 0.0
Circle Area: 0.0\n
Rectangle Area: 0.0
Circle Area: 0.0\n
Rectangle Area: 0.0
Passed






Week 7 : Programming Assignment 5

Due on 2024-03-14, 23:59 IST
Two interfaces are provided, namely Flyable and Swimmable,
Your task is to make the following:
  • Create a class "FlyingFish" that implements both interfaces and provides its own implementation for fly() and swim()
  • It should have a private variable name, use a constructor to set the value and the functions fly() and swim() to print exactly as given in the test case.
Follow the naming convention as given in the main method of the suffix code.
Your last recorded submission was on 2024-03-03, 12:31 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
3
interface Flyable {
4
    void fly();
5
}
6
7
interface Swimmable {
8
    void swim();
9
}
10
// TODO: Create a class "FlyingFish" that implements both "Flyable" and "Swimmable" interfaces.
11
class FlyingFish implements Flyable,Swimmable
12
{
13
  private String name;
14
    FlyingFish(String name) {
15
     this.name = name;
16
    }
17
 
18
    @Override
19
    public void fly() {
20
        System.out.println(name + " can glide through the air");
21
    }
22
 
23
    @Override
24
    public void swim() {
25
        System.out.print(name + " can swim in water");
26
    }
27
}
0
public class NPTEL {
1
    public static void main(String[] args) {
2
        Scanner scanner = new Scanner(System.in);
3
4
        // System.out.print("Enter the name of the fish: ");
5
        String fishName = scanner.nextLine();
6
7
        FlyingFish flyingFish = new FlyingFish(fishName);
8
9
        // Test the FlyingFish class
10
        flyingFish.fly();
11
        flyingFish.swim();
12
13
        scanner.close();
14
    }
15
}
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.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
John
John can glide through the air\n
John can swim in water
John can glide through the air\n
John can swim in water
Passed






Private Test cases used for EvaluationStatus
Test Case 1
Passed














Week 8 : Programming Assignment 1

Due on 2024-03-21, 23:59 IST
Create a class called Animal with protected fields and a parameterized constructor to initialize the protected fields.
Fields:
  • name (String)
  • sound (String)
and Method:
  • public void makeSound()  - This method should display the name of the animal and the sound it makes.
Create a class called Dog that extends the Animal class.
The Dog class should have a constructor that accepts the name of the dog and sets the sound to "Woof".

Create a class called Cat that extends the Animal class.
The Cat class should have a constructor that accepts the name of the cat and sets the sound to "Meow".

Follow the naming convention as given in the main method of the suffix code.
Your last recorded submission was on 2024-03-09, 22:57 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
// TODO: Create a class "Animal" that have a  parameterized constructor to initialize the protected fields and method of makeSound().
3
// Create a class called Dog that extends the Animal class.
4
// Create a class called Cat that extends the Animal class.
5
class Animal {
6
  protected String name;
7
  protected String sound;
8
  
9
  public Animal(String name, String sound) {
10
     this.name = name;
11
     this.sound = sound;
12
  }
13
  public void makeSound() {
14
     System.out.println(this.name + " says: " + this.sound);
15
  }
16
}
17
class Dog extends Animal {
18
    public Dog(String name) {
19
      super(name, "Woof");
20
    }
21
}
22
class Cat extends Animal {
23
    public Cat(String name) {
24
      super(name, "Meow");
25
    }
26
}
0
class W08_P1{
1
    public static void main(String[] args) {
2
        Scanner scanner = new Scanner(System.in);
3
4
        String dogName = scanner.nextLine();
5
        String catName = scanner.nextLine();
6
7
        Dog dog = new Dog(dogName);
8
        Cat cat = new Cat(catName);
9
10
        dog.makeSound(); 
11
        cat.makeSound();  
12
13
        scanner.close();
14
    }
15
}
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.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
Jacky
Tom
Jacky says: Woof\n
Tom says: Meow
Jacky says: Woof\n
Tom says: Meow\n
Passed after ignoring Presentation Error





Week 8 : Programming Assignment 2

Due on 2024-03-21, 23:59 IST
Program to Generate Harmonic Series.
Follow the naming convention as given in the main method of the suffix code.
Your last recorded submission was on 2024-03-09, 23:22 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
class W08_P2
3
{
4
public static void main(String... a)
5
    {        
6
        Scanner s = new Scanner(System.in);
7
        int num = s.nextInt();
8
//TODO code to generate harmonic series using while loops.
9
        double result = 0.0;
10
        while(num > 0)
11
          {
12
               result = result + (double) 1 / num;
13
               num--;
14
          }
15
        System.out.println("Output of Harmonic Series is "+result);
0
}
1
}
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.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
6
Output of Harmonic Series is 2.45
Output of Harmonic Series is 2.45\n
Passed after ignoring Presentation Error







Week 8 : Programming Assignment 3

Due on 2024-03-21, 23:59 IST
Write a code to find the Trace of the 2D matrix.
Your last recorded submission was on 2024-03-10, 07:50 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.*;
2
3
public class W08_P3{
4
    public static void main(String args[]) {
5
        int array[][] = new int[5][5];
6
        int i, j;
7
        double sum = 0, square = 0, result = 0;
8
        Scanner s = new Scanner(System.in);
9
 
10
        // reads number of rows from the user
11
        int row = s.nextInt();
12
13
        // reads number of columns from the user
14
        int column = s.nextInt();
15
        for (i = 0; i < row; i++) {
16
            // loop for columns
17
            for (j = 0; j < column; j++) {
18
                // reads the matrix elements
19
                array[i][j] = s.nextInt();
20
                // prints space
21
               // System.out.print(" ");
22
            }
23
        }
24
// Input 2D matrix using Scanner Class 
25
//Calculate the trace of the matrix
26
for (i = 0; i < row; i++) {
27
            // loop for columns
28
            for (j = 0; j < column; j++) {
29
                if( i == j )
30
                    sum+= array[i][j];
31
                // prints space
32
               // System.out.print(" ");
33
            }
34
        }
35
System.out.print(sum);
0
} 
1
}
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.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
5
5
2 3 5 6 7
8 9 10 11 12
13 14 15 16 17
18 1 3 0 6
7 8 11 8 11
37.0
37.0
Passed






Week 8 : Programming Assignment 4

Due on 2024-03-21, 23:59 IST
Program to remove duplicate elements from sorted array
Follow the naming convention as given in the main method of the suffix code.
Your last recorded submission was on 2024-03-10, 10:02 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
class W08_P4
3
{
4
//TODO code to remove duplicates from sorted array
5
static int removeDuplicates(int arr[])
6
    {
7
        int n = arr.length;
8
        // Return, if array is empty or 
9
        // contains a single element
10
        if (n == 0 || n == 1)
11
            return n;
12
 
13
        int[] temp = new int[n];
14
 
15
        // Start traversing elements
16
        int j = 0;
17
        for (int i = 0; i < n - 1; i++)
18
             
19
            // If current element is not equal to next
20
            // element then store that current element
21
            if (arr[i] != arr[i + 1])
22
                temp[j++] = arr[i];
23
 
24
        // Store the last element as whether it is unique or
25
        // repeated, it hasn't stored previously
26
        temp[j++] = arr[n - 1];
27
 
28
        // Modify original array
29
        for (int i = 0; i < j; i++)
30
            arr[i] = temp[i];
31
 
32
        return j;
33
    }
0
public static void main(String... a)
1
    {        
2
        Scanner s = new Scanner(System.in);
3
        int size = s.nextInt();
4
        int[] array = new int[size];
5
        int i;
6
        for (i = 0; i < size; i++) {
7
            array[i] = s.nextInt();
8
        }
9
        int index = removeDuplicates(array);
10
        for(i=0; i<index; i++){
11
            System.out.print(array[i] + " ");
12
        }
13
    }
14
}
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.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
6
1 2 2 3 3 4
1 2 3 4
1 2 3 4 
Passed after ignoring Presentation Error










Week 8 : Programming Assignment 5

Due on 2024-03-21, 23:59 IST
Write a code to find the Determinant of the 2D matrix.
Your last recorded submission was on 2024-03-10, 09:53 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.*;
2
3
public class W08_P5{
4
static int determinantOfMatrix(int mat[][], int n)
5
    {
6
        int num1, num2, det = 1, index,
7
                        total = 1; // Initialize result
8
         int[] temp = new int[n + 1];
9
         for (int i = 0; i < n; i++){
10
            index = i; // initialize the index
11
             while (index < n && mat[index][i] == 0){
12
                index++;
13
            }
14
            if (index == n) // if there is non zero element
15
            {
16
                continue;
17
            }
18
            if (index != i){
19
                for (int j = 0; j < n; j++){
20
                    swap(mat, index, j, i, j);
21
                }
22
                det = (int)(det * Math.pow(-1, index - i));
23
            }
24
             for (int j = 0; j < n; j++){
25
                temp[j] = mat[i][j];
26
            }
27
            for (int j = i + 1; j < n; j++){
28
                num1 = temp[i]; // value of diagonal element
29
                num2 = mat[j][i]; // value of next row element
30
                 for (int k = 0; k < n; k++) {
31
                    mat[j][k] = (num1 * mat[j][k])
32
                                - (num2 * temp[k]);
33
                }
34
                total = total * num1; // Det(kA)=kDet(A);
35
            }
36
        }
37
        for (int i = 0; i < n; i++) {
38
            det = det * mat[i][i];
39
        }
40
        return (det / total); // Det(kA)/k=Det(A);
41
    }
42
    static int[][] swap(int[][] arr, int i1, int j1, int i2,
43
                        int j2){
44
        int temp = arr[i1][j1];
45
        arr[i1][j1] = arr[i2][j2];
46
        arr[i2][j2] = temp;
47
        return arr;
48
}
0
public static void main(String args[]) {
1
        int array[][] = new int[5][5];
2
        int i, j;
3
       // double sum = 0, square = 0, result = 0;
4
        Scanner s = new Scanner(System.in);
5
 
6
        
7
        int dimension = s.nextInt();
8
9
       
10
        for (i = 0; i < dimension ; i++) {
11
            // loop for columns
12
            for (j = 0; j < dimension ; j++) {
13
                // reads the matrix elements
14
                array[i][j] = s.nextInt();
15
                // prints space
16
               // System.out.print(" ");
17
            }
18
        }
19
System.out.printf("Determinant of the matrix is : %d",
20
            determinantOfMatrix(array,dimension));
21
} 
22
}
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.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
4
1 0 2 -1
3 0 0 5
2 1 4 -3
1 0 5 0
Determinant of the matrix is : 30
Determinant of the matrix is : 30
Passed








































No comments:

Post a Comment

Keep your comments reader friendly. Be civil and respectful. No self-promotion or spam. Stick to the topic. Questions welcome.