NPTEL
Code compiled and tested successfully!
Due date on 2026-08-14, 23:59 IST
Complete Program
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
// --- 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 ---
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
Note: These tests may not be considered while scoring.
Due date on 2026-08-14, 23:59 IST
Complete Program
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
// --- 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 ---
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
Note: These tests may not be considered while scoring.
Due date on 2026-08-14, 23:59 IST
Complete Program
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
// --- START OF SOLUTION CODE ---
int count = 0;
while (num != 0) {
num /= 10;
++count;
}
System.out.print(count);
// --- END OF SOLUTION CODE ---
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
Note: These tests may not be considered while scoring.
Due date on 2026-08-14, 23:59 IST
Complete Program
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
// --- 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 ---
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
Note: These tests may not be considered while scoring.
Due date on 2026-08-14, 23:59 IST
Complete Program
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
// --- START OF SOLUTION CODE ---
for (int i = 1; i <= num; i++) {
if (num % i == 0) {
System.out.print(i + " ");
}
}
// --- END OF SOLUTION CODE ---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
Note: These tests may not be considered while scoring.
Due date on 2026-08-20, 23:59 IST
Complete Program
Javaimport java.util.Scanner;
public class W04_P1 {
// Declare a class Student with one member variable of default access
static class Student {
int rollNo; // Default access modifier (no keyword written)
// Constructor to assign rollNo
Student(int r) {
rollNo = r;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Read roll number
int r = sc.nextInt();
// Create Student object with roll number
Student s = new Student(r);
// --- START OF SOLUTION CODE ---
System.out.println("Roll Number is: " + s.rollNo);
// --- END OF SOLUTION CODE ---
sc.close();
}
}
Code Snippet to Paste in the Editor
Java// --- START OF SOLUTION CODE ---
System.out.println("Roll Number is: " + s.rollNo);
// --- END OF SOLUTION CODE ---
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
Note: These tests may not be considered while scoring.
Due date on 2026-08-20, 23:59 IST
Complete Program
import java.util.Scanner;
public class W04_P2 {
// Declare class Car with a public member variable
static class Car {
public int speed; // Public access, can be accessed from outside the class
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Read speed value
int s = sc.nextInt();
// Create Car object
Car c = new Car();
// Assign speed to the object
c.speed = s;
// --- START OF SOLUTION CODE ---
System.out.println("Speed is: " + c.speed);
// --- END OF SOLUTION CODE ---
sc.close();
}
}
Code Snippet to Paste in the Editor
// --- START OF SOLUTION CODE ---
System.out.println("Speed is: " + c.speed);
// --- END OF SOLUTION CODE ---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
Note: These tests may not be considered while scoring.
Due date on 2026-08-20, 23:59 IST
Complete Program
import java.util.Scanner;
public class W04_P3 {
// Declare class Account with a private member variable
static class Account {
private int balance; // Private member, cannot be accessed directly from outside
// Method to set balance value
public void setBalance(int b) {
balance = b;
}
// --- START OF SOLUTION CODE ---
public int getBalance() {
return balance;
}
// --- END OF SOLUTION CODE ---
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Read balance value
int b = sc.nextInt();
// Create Account object
Account acc = new Account();
// Set balance using method
acc.setBalance(b);
// Print balance using the method
System.out.println("Account Balance is: " + acc.getBalance());
sc.close();
}
}
Code Snippet to Paste in the Editor
// --- START OF SOLUTION CODE ---
public int getBalance() {
return balance;
}
// --- END OF SOLUTION CODE ---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
Note: These tests may not be considered while scoring.
Due date on 2026-08-20, 23:59 IST
Complete Program
import java.util.Scanner;
public class W04_P4 {
// Declare parent class Employee with a protected member variable
static class Employee {
protected int salary; // Protected member
}
// Subclass Manager inherits from Employee
static class Manager extends Employee {
// No additional members required for this task
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Read salary value
int s = sc.nextInt();
// Create Manager object
Manager m = new Manager();
// --- START OF SOLUTION CODE ---
m.salary = s;
System.out.println("Salary is: " + m.salary);
// --- END OF SOLUTION CODE ---
sc.close();
}
}
Code Snippet to Paste in the Editor
// --- START OF SOLUTION CODE ---
m.salary = s;
System.out.println("Salary is: " + m.salary);
// --- END OF SOLUTION CODE ---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
Note: These tests may not be considered while scoring.
Due date on 2026-08-20, 23:59 IST
Complete Program
import java.util.Scanner;
public class W04_P5 {
// Declare Calculator class with overloaded add methods
static class Calculator {
// Public method to add two integers
public int add(int a, int b) {
return a + b;
}
// --- START OF SOLUTION CODE ---
private int add(int a, int b, int c) {
return a + b + c;
}
// --- END OF SOLUTION CODE ---
// Method to demonstrate private method access within class
public void printThreeSum(int x, int y, int z) {
int sum = add(x, y, z); // Call private method within class
System.out.println("Sum of three numbers: " + sum);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Read two numbers
int a = sc.nextInt();
int b = sc.nextInt();
// Read three numbers
int x = sc.nextInt();
int y = sc.nextInt();
int z = sc.nextInt();
Calculator calc = new Calculator();
// Call public method to add two numbers
int sumTwo = calc.add(a, b);
System.out.println("Sum of two numbers: " + sumTwo);
// Call method that prints sum of three numbers
calc.printThreeSum(x, y, z);
sc.close();
}
}
Code Snippet to Paste in the Editor
// --- START OF SOLUTION CODE ---
private int add(int a, int b, int c) {
return a + b + c;
}
// --- END OF SOLUTION CODE ---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
Note: These tests may not be considered while scoring.
Due date on 2026-08-27, 23:59 IST
Complete Program
import java.util.Scanner;
interface Number {
int findSqr(int i); // Returns the square of n
}
// --- START OF SOLUTION CODE ---
class A implements Number {
public int findSqr(int i) {
return i * i;
}
}
// --- END OF SOLUTION CODE ---
public class W05_P1{
public static void main (String[] args){
A a = new A(); //Create an object of class A
// Read a number from the keyboard
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
System.out.print(a.findSqr(i));
}
}
Code Snippet to Paste in the Editor
// --- START OF SOLUTION CODE ---
class A implements Number {
public int findSqr(int i) {
return i * i;
}
}
// --- END OF SOLUTION CODE ---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
Note: These tests may not be considered while scoring.
Due date on 2026-08-27, 23:59 IST
Complete Program
import java.util.Scanner;
interface GCD {
public int findGCD(int n1,int n2);
}
// --- START OF SOLUTION CODE ---
class B implements GCD {
public int findGCD(int n1, int n2) {
if (n1 < 0 || n2 < 0) {
return -1;
}
if (n1 == 0 && n2 == 0) {
return 0;
}
else if (n2 == 0) {
return n1;
}
else {
return findGCD(n2, n1 % n2);
}
}
}
// --- END OF SOLUTION CODE ---
public class W05_P2{
public static void main (String[] args){
B a = new B(); //Create an object of class B
// Read two numbers from the keyboard
Scanner sc = new Scanner(System.in);
int p1 = sc.nextInt();
int p2 = sc.nextInt();
System.out.print(a.findGCD(p1,p2));
}
}
Code Snippet to Paste in the Editor
// --- START OF SOLUTION CODE ---
class B implements GCD {
public int findGCD(int n1, int n2) {
if (n1 < 0 || n2 < 0) {
return -1;
}
if (n1 == 0 && n2 == 0) {
return 0;
}
else if (n2 == 0) {
return n1;
}
else {
return findGCD(n2, n1 % n2);
}
}
}
// --- END OF SOLUTION CODE ---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
Note: These tests may not be considered while scoring.
Due date on 2026-08-27, 23:59 IST
Complete Program
import java.util.Scanner;
public class W05_P3 {
public static void main(String[] args) {
int a, b;
Scanner input = new Scanner(System.in);
// Read any two values for a and b
int result;
a = input.nextInt();
b = input.nextInt();
// --- START OF SOLUTION CODE ---
try {
result = a / b;
System.out.print(result);
} catch (ArithmeticException e) {
System.out.print("Exception caught: Division by zero.");
}
// --- END OF SOLUTION CODE ---
}
}
Code Snippet to Paste in the Editor
// --- START OF SOLUTION CODE ---
try {
result = a / b;
System.out.print(result);
} catch (ArithmeticException e) {
System.out.print("Exception caught: Division by zero.");
}
// --- END OF SOLUTION CODE ---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
Note: These tests may not be considered while scoring.
Due date on 2026-08-27, 23:59 IST
Complete Program
//Prefixed Fixed Code:
import java.util.Scanner;
import java.util.InputMismatchException;
public class W05_P4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int length = sc.nextInt();
// create an array to save user input
int[] name = new int[length];
int sum=0;//save the total sum of the array.
// --- START OF SOLUTION CODE ---
try {
for (int i = 0; i < length; i++) {
name[i] = sc.nextInt();
sum += name[i];
}
System.out.print(sum);
} catch (InputMismatchException e) {
System.out.print("You entered bad data.");
}
// --- END OF SOLUTION CODE ---
}
}
Code Snippet to Paste in the Editor
// --- START OF SOLUTION CODE ---
try {
for (int i = 0; i < length; i++) {
name[i] = sc.nextInt();
sum += name[i];
}
System.out.print(sum);
} catch (InputMismatchException e) {
System.out.print("You entered bad data.");
}
// --- END OF SOLUTION CODE ---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
Note: These tests may not be considered while scoring.
Due date on 2026-08-27, 23:59 IST
Complete Program
import java.util.Scanner;
public class W05_P5{
public static void main (String args[ ] ) {
Scanner scan = new Scanner(System.in);
int i=scan.nextInt();
int j;
// --- START OF SOLUTION CODE ---
try {
switch (i) {
case 0 :
int zero = 0;
j = 92/ zero;
break;
case 1:
int b[ ] = null;
j = b[0] ;
break;
default:
System.out.print("No exception");
}
} catch (Exception e) {
System.out.print(e);
}
// --- END OF SOLUTION CODE ---
}
}
Code Snippet to Paste in the Editor
// --- START OF SOLUTION CODE ---
try {
switch (i) {
case 0 :
int zero = 0;
j = 92/ zero;
break;
case 1:
int b[ ] = null;
j = b[0] ;
break;
default:
System.out.print("No exception");
}
} catch (Exception e) {
System.out.print(e);
}
// --- END OF SOLUTION CODE ---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
Note: These tests may not be considered while scoring.
Due date on 2026-09-03, 23:59 IST
Complete Program
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 {
// --- START OF SOLUTION CODE ---
int result = num1 / num2;
System.out.println("Result is: " + result);
// --- END OF SOLUTION CODE ---
} catch (ArithmeticException e) {
// Print safe message if division by zero occurs
System.out.println("Cannot divide by zero");
}
sc.close();
}
}
Code Snippet to Paste in the Editor
// --- START OF SOLUTION CODE ---
int result = num1 / num2;
System.out.println("Result is: " + result);
// --- END OF SOLUTION CODE ---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
Note: These tests may not be considered while scoring.
Due date on 2026-09-03, 23:59 IST
Complete Program
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 {
// --- START OF SOLUTION CODE ---
int result = num1 / num2;
System.out.println("Division successful");
System.out.println("Result is: " + result);
// --- END OF SOLUTION CODE ---
} 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();
}
}
Code Snippet to Paste in the Editor
// --- START OF SOLUTION CODE ---
int result = num1 / num2;
System.out.println("Division successful");
System.out.println("Result is: " + result);
// --- END OF SOLUTION CODE ---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
Note: These tests may not be considered while scoring.
Due date on 2026-09-03, 23:59 IST
Complete Program
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 {
// --- START OF SOLUTION CODE ---
int result = num1 / num2;
System.out.println("Division successful");
System.out.println("Result is: " + result);
// --- END OF SOLUTION CODE ---
} 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();
}
}
Code Snippet to Paste in the Editor
// --- START OF SOLUTION CODE ---
int result = num1 / num2;
System.out.println("Division successful");
System.out.println("Result is: " + result);
// --- END OF SOLUTION CODE ---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
Note: These tests may not be considered while scoring.
Due date on 2026-09-03, 23:59 IST
Complete Program
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 {
// --- START OF SOLUTION CODE ---
int result = num1 / num2;
System.out.println("Result is: " + result);
// --- END OF SOLUTION CODE ---
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
} finally {
// Print final message, runs always
System.out.println("Program Ended");
}
sc.close();
}
}
Code Snippet to Paste in the Editor
// --- START OF SOLUTION CODE ---
int result = num1 / num2;
System.out.println("Result is: " + result);
// --- END OF SOLUTION CODE ---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
Note: These tests may not be considered while scoring.
Due date on 2026-09-03, 23:59 IST
Complete Program
import java.util.Scanner;
public class W06_P5 {
// Method to calculate square root, may throw Exception
public static double calculateSquareRoot(double num) throws Exception {
// --- START OF SOLUTION CODE ---
if (num < 0) {
throw new Exception("Number cannot be negative");
}
return Math.sqrt(num);
// --- END OF SOLUTION CODE ---
}
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();
}
}
Code Snippet to Paste in the Editor
// --- START OF SOLUTION CODE ---
if (num < 0) {
throw new Exception("Number cannot be negative");
}
return Math.sqrt(num);
// --- END OF SOLUTION CODE ---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
Note: These tests may not be considered while scoring.
Due date on 2026-09-10, 23:59 IST
Complete Program
import java.util.Scanner; public class W07_P1 { // --- START OF SOLUTION CODE --- public static String findLongestWord(String text) { String longestWord = ""; String[] words = text.split("\\s+"); for (String word : words) { if (word.length() > longestWord.length()) { longestWord = word; } } return longestWord; } // --- END OF SOLUTION CODE --- public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String text = scanner.nextLine(); scanner.close(); String longestWord = findLongestWord(text); System.out.println("The longest word in the text is: " + longestWord); } }
Code Snippet to Paste in the Editor
// --- START OF SOLUTION CODE --- public static String findLongestWord(String text) { String longestWord = ""; String[] words = text.split("\\s+"); for (String word : words) { if (word.length() > longestWord.length()) { longestWord = word; } } return longestWord; } // --- END OF SOLUTION CODE ---
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
Note: These tests may not be considered while scoring.
Due date on 2026-09-10, 23:59 IST
Complete Program
import java.util.*; public class W07_P2 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int[] array = new int[n]; for (int i = 0; i < n; i++) { array[i] = scanner.nextInt(); } int elementToRemove = scanner.nextInt(); scanner.close(); System.out.println("Original Array: " + Arrays.toString(array)); array = removeAll(array, elementToRemove); System.out.print("Array after removing " + elementToRemove + ": " + Arrays.toString(array)); } // --- START OF SOLUTION CODE --- public static int[] removeAll(int[] array, int elementToRemove) { int[] result = new int[array.length]; int index = 0; for (int value : array) { if (value != elementToRemove) { result[index++] = value; } } return Arrays.copyOf(result, index); } // --- END OF SOLUTION CODE --- }
Code Snippet to Paste in the Editor
// --- START OF SOLUTION CODE --- public static int[] removeAll(int[] array, int elementToRemove) { int[] result = new int[array.length]; int index = 0; for (int value : array) { if (value != elementToRemove) { result[index++] = value; } } return Arrays.copyOf(result, index); } // --- END OF SOLUTION CODE ---
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
Note: These tests may not be considered while scoring.
Due date on 2026-09-10, 23:59 IST
Complete Program
import java.util.Scanner; public class W07_P3 { // --- START OF SOLUTION CODE --- public static boolean isPrime(int n) { if (n < 2) return false; for (int i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } public static int primeSum(int x, int y) { int sum = 0; int start = Math.min(x, y); int end = Math.max(x, y); for (int i = start; i <= end; i++) { if (isPrime(i)) { sum += i; } } return sum; } // --- END OF SOLUTION CODE --- public static void main(String[] args) { Scanner sc = new Scanner(System.in); int x = sc.nextInt(); int y = sc.nextInt(); System.out.println(primeSum(x, y)); } }
Code Snippet to Paste in the Editor
// --- START OF SOLUTION CODE --- public static boolean isPrime(int n) { if (n < 2) return false; for (int i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } public static int primeSum(int x, int y) { int sum = 0; int start = Math.min(x, y); int end = Math.max(x, y); for (int i = start; i <= end; i++) { if (isPrime(i)) { sum += i; } } return sum; } // --- END OF SOLUTION CODE ---
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
Note: These tests may not be considered while scoring.
Due date on 2026-09-10, 23:59 IST
Complete Program
import java.util.Scanner; class PrintNumbers implements Runnable { // --- START OF SOLUTION CODE --- private int start; private int end; public PrintNumbers(int start, int end) { this.start = start; this.end = end; } @Override public void run() { for (int i = start; i <= end; i += 2) { System.out.println(Thread.currentThread().getName() + ": " + i); } } // --- END OF SOLUTION CODE --- } class W07_P4 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int evenStart = scanner.nextInt(); int evenEnd = scanner.nextInt(); int oddStart = scanner.nextInt(); int oddEnd = scanner.nextInt(); Thread evenThread = new Thread(new PrintNumbers(evenStart, evenEnd), "EvenThread"); Thread oddThread = new Thread(new PrintNumbers(oddStart, oddEnd), "OddThread"); try { evenThread.start(); evenThread.join(); oddThread.start(); oddThread.join(); } catch (InterruptedException e) { e.printStackTrace(); } scanner.close(); } }
Code Snippet to Paste in the Editor
// --- START OF SOLUTION CODE --- private int start; private int end; public PrintNumbers(int start, int end) { this.start = start; this.end = end; } @Override public void run() { for (int i = start; i <= end; i += 2) { System.out.println(Thread.currentThread().getName() + ": " + i); } } // --- END OF SOLUTION CODE ---
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
Note: These tests may not be considered while scoring.
Due date on 2026-09-10, 23:59 IST
Complete Program
import java.util.Scanner; public class W07_P5 { private String password; public W07_P5(String password) { this.password = password; } // --- START OF SOLUTION CODE --- public boolean isValidPassword(String password) { if (password == null || password.length() < 8) { return false; } boolean hasUpper = false; boolean hasDigit = false; for (int i = 0; i < password.length(); i++) { char ch = password.charAt(i); if (Character.isUpperCase(ch)) { hasUpper = true; } if (Character.isDigit(ch)) { hasDigit = true; } } return hasUpper && hasDigit; } public boolean isValidPassword() { return isValidPassword(this.password); } // --- END OF SOLUTION CODE --- public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String inputPassword = scanner.nextLine(); scanner.close(); W07_P5 validator = new W07_P5(inputPassword); if (validator.isValidPassword(inputPassword)) { System.out.print("Valid Password"); } else { System.out.print("Invalid Password"); } } }
Code Snippet to Paste in the Editor
// --- START OF SOLUTION CODE --- public boolean isValidPassword(String password) { if (password == null || password.length() < 8) { return false; } boolean hasUpper = false; boolean hasDigit = false; for (int i = 0; i < password.length(); i++) { char ch = password.charAt(i); if (Character.isUpperCase(ch)) { hasUpper = true; } if (Character.isDigit(ch)) { hasDigit = true; } } return hasUpper && hasDigit; } public boolean isValidPassword() { return isValidPassword(this.password); } // --- END OF SOLUTION CODE ---
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
Note: These tests may not be considered while scoring.
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.