1.
Duck Number
import [Link];
class DuckNumber {
void checkDuck(int num) {
String str = [Link](num);
if ([Link](0) == '0') {
[Link]("Number must not begin with zero.");
return;
int found = 0;
for (int i = 1; i < [Link](); i++) {
if ([Link](i) == '0') {
found = 1;
if (found == 1) {
[Link]("It is a Duck number.");
} else {
[Link]("It is not a Duck number.");
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
DuckNumber obj = new DuckNumber();
[Link]("Enter a number: ");
int num = [Link]();
[Link](num);
}
}
Q2
import [Link];
class MagicNumber {
int digitSum(int num) {
while (num > 9) {
int sum = 0;
while (num > 0) {
sum = sum + num % 10;
num = num / 10;
num = sum;
return num;
void displayMagic(int p, int q) {
for (int i = p; i <= q; i++) {
if (digitSum(i) == 1) {
[Link](i);
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
MagicNumber obj = new MagicNumber();
[Link]("Enter value of p: ");
int p = [Link]();
[Link]("Enter value of q: ");
int q = [Link]();
[Link](p, q);
}
Q3
import [Link];
class NumberCheck {
void checkPalindrome(int n) {
int rev = 0, temp = n;
while (temp > 0) {
rev = rev * 10 + temp % 10;
temp = temp / 10;
if (rev == n) {
[Link]("Palindrome Number");
} else {
[Link]("Not a Palindrome Number");
void checkPerfect(int n) {
int sum = 0;
for (int i = 1; i < n; i++) {
if (n % i == 0) {
sum = sum + i;
if (sum == n) {
[Link]("Perfect Number");
} else {
[Link]("Not a Perfect Number");
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
NumberCheck obj = new NumberCheck();
[Link]("1. Palindrome");
[Link]("2. Perfect Number");
[Link]("Enter your choice: ");
int ch = [Link]();
[Link]("Enter a number: ");
int num = [Link]();
if (ch == 1) {
[Link](num);
} else if (ch == 2) {
[Link](num);
} else {
[Link]("Invalid Choice");
}
Q4
import [Link];
class Triangle {
void checkTriangle(int a, int b, int c) {
if (a + b + c == 180) {
if (a < 90 && b < 90 && c < 90) {
[Link]("Acute-angled Triangle");
} else if (a == 90 || b == 90 || c == 90) {
[Link]("Right-angled Triangle");
} else {
[Link]("Obtuse-angled Triangle");
} else {
[Link]("Triangle not possible");
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
Triangle obj = new Triangle();
[Link]("Enter three angles: ");
int a = [Link]();
int b = [Link]();
int c = [Link]();
[Link](a, b, c);
}
Q5
import [Link];
class Showroom {
void calculateBill(int cost) {
int discount = 0;
String gift = "";
if (cost <= 2000) {
discount = 5;
gift = "Calculator";
} else if (cost <= 5000) {
discount = 10;
gift = "School Bag";
} else if (cost <= 10000) {
discount = 15;
gift = "Wall Clock";
} else {
discount = 20;
gift = "Wrist Watch";
double finalAmount = cost - (cost * discount / 100.0);
[Link]("Amount to be paid: Rs." + finalAmount);
[Link]("Gift: " + gift);
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
Showroom obj = new Showroom();
[Link]("Enter total cost: ");
int cost = [Link]();
[Link](cost);
}
Q6
class Series {
void series1() {
for (int i = 2; i <= 10; i++) {
[Link]((i * i) + " ");
[Link]();
void series2() {
for (int i = 1; i <= 10; i++) {
[Link](i + "/" + (i * 3) + ", ");
[Link]();
void series3(int x, int n) {
int sum = 0;
for (int i = 1; i <= n; i++) {
sum = sum + i * x * x;
[Link]("Sum: " + sum);
void series4() {
int s = 0, term = 1;
for (int i = 1; i <= 5; i++) {
s = s + term;
[Link](term + " ");
term = term * 10 + 1;
}
[Link]("\nSum: " + s);
public static void main(String[] args) {
Series obj = new Series();
obj.series1();
obj.series2();
obj.series3(2, 4); // you can change values
obj.series4();
}
Q7
import [Link];
class Fibonacci {
void printFibonacci(int limit) {
int a = 0, b = 1, c;
[Link](a + " " + b + " ");
c = a + b;
while (c <= limit) {
[Link](c + " ");
a = b;
b = c;
c = a + b;
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
Fibonacci obj = new Fibonacci();
[Link]("Enter limit: ");
int n = [Link]();
[Link](n);
}
Q8
import [Link];
class Calculator {
void operate(double a, double b, char op) {
double result = 0;
if (op == '+') {
result = a + b;
} else if (op == '-') {
result = a - b;
} else if (op == '*') {
result = a * b;
} else if (op == '/') {
if (b != 0) {
result = a / b;
} else {
[Link]("Cannot divide by zero.");
return;
} else {
[Link]("Invalid Operator");
return;
[Link](a + " " + op + " " + b + ": " + result);
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
Calculator obj = new Calculator();
[Link]("Enter first number: ");
double x = [Link]();
[Link]("Enter second number: ");
double y = [Link]();
[Link]("Enter operator (+, -, *, /): ");
char op = [Link]().charAt(0);
[Link](x, y, op);
}
Q9
import [Link];
class CommonCharRemove {
void removeCommon(String s1, String s2) {
String result1 = "";
String result2 = "";
for (int i = 0; i < [Link](); i++) {
char ch = [Link](i);
if ([Link](ch) == -1) {
result1 = result1 + ch;
for (int i = 0; i < [Link](); i++) {
char ch = [Link](i);
if ([Link](ch) == -1) {
result2 = result2 + ch;
[Link]("First String: " + result1);
[Link]("Second String: " + result2);
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
CommonCharRemove obj = new CommonCharRemove();
[Link]("Enter first string: ");
String a = [Link]();
[Link]("Enter second string: ");
String b = [Link]();
[Link](a, b);
}
}
Q10
class Patterns {
void pattern1() {
for (int i = 5; i >= 1; i--) {
for (int j = i; j <= 5 + i - 1; j++) {
[Link](j + " ");
[Link]();
void pattern2() {
for (int i = 1; i <= 5; i++) {
for (int j = 5; j > i; j--) {
[Link](" ");
for (int j = 1; j <= i; j++) {
[Link](j + " ");
[Link]();
void pattern3() {
for (int i = 4; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
[Link]("@ ");
[Link]();
for (int i = 2; i <= 4; i++) {
for (int j = 1; j <= i; j++) {
[Link]("@ ");
[Link]();
void pattern4() {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++) {
if (i == j) {
[Link]("/ ");
} else {
[Link]("* ");
[Link]();
public static void main(String[] args) {
Patterns obj = new Patterns();
obj.pattern1();
obj.pattern2();
obj.pattern3();
obj.pattern4();
}
Q11
class Pizza {
String pizzaSize;
int cheese, pepperoni, mushroom;
Pizza(String size, int c, int p, int m) {
pizzaSize = size;
cheese = c;
pepperoni = p;
mushroom = m;
double calculateCost() {
int toppingCount = cheese + pepperoni + mushroom;
int base = 0;
if ([Link]("small")) {
base = 500;
if ([Link]("medium")) {
base = 650;
if ([Link]("large")) {
base = 800;
return base + (toppingCount * 25);
String pizzaDescription() {
return "Size: " + pizzaSize + "\nCheese: " + cheese +
"\nPepperoni: " + pepperoni + "\nMushroom: " + mushroom +
"\nCost: Rs." + calculateCost();
}
public static void main(String[] args) {
Pizza obj = new Pizza("medium", 2, 1, 1);
[Link]([Link]());
}
Q12
class Calculate {
int num, f, rev;
void calculate(int n) {
num = n;
f = 0;
rev = 0;
int prime() {
int i = 2;
while (i < num) {
if (num % i == 0) {
return 0;
i++;
return 1;
int reverse() {
int temp = num;
while (temp > 0) {
rev = rev * 10 + temp % 10;
temp = temp / 10;
return rev;
void display() {
reverse();
if (prime() == 1 && rev == num) {
[Link]("Prime Palindrome");
} else {
[Link]("Not Prime Palindrome");
public static void main(String[] args) {
Calculate obj = new Calculate();
[Link](131);
[Link]();
}
Q13
class BankAccount {
String name;
long accNumber;
char type;
double balance;
BankAccount() {
name = "";
accNumber = 0;
type = ' ';
balance = 0;
BankAccount(String n, long acc, char t, double b) {
name = n;
accNumber = acc;
type = t;
balance = b;
void deposit(double amt) {
balance = balance + amt;
void withdraw(double amt) {
if (amt <= balance) {
balance = balance - amt;
} else {
[Link]("Insufficient Balance");
}
}
void display() {
[Link]("Name: " + name);
[Link]("Balance: " + balance);
public static void main(String[] args) {
BankAccount obj = new BankAccount("Ravi", 123456, 'S', 1000);
[Link](500);
[Link](200);
[Link]();
}
Q14
class VolumeCalc {
double volume(double r) {
return (4 * 22 * r * r * r) / (3 * 7);
double volume(double h, double r) {
return (22 * r * r * h) / 7;
double volume(double l, double b, double h) {
return l * b * h;
public static void main(String[] args) {
VolumeCalc obj = new VolumeCalc();
[Link]("Sphere: " + [Link](5));
[Link]("Cylinder: " + [Link](10, 3));
[Link]("Cuboid: " + [Link](4, 3, 2));
}
Q15
class SquareOdd {
void squareOdd() {
int[][] arr = {
{1, 4, 5},
{2, 3, 7}
};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
if (arr[i][j] % 2 != 0) {
[Link]("Square of " + arr[i][j] + " = " +
(arr[i][j] * arr[i][j]));
public static void main(String[] args) {
SquareOdd obj = new SquareOdd();
[Link]();
}
Q16
import [Link];
class CityNames {
void printCities(String[] cities) {
for (int i = 0; i < [Link]; i++) {
char first = [Link](cities[i].charAt(0));
char last =
[Link](cities[i].charAt(cities[i].length() - 1));
if (first != 'a' && first != 'e' && first != 'i' && first !=
'o' && first != 'u') {
if (last == 'a' || last == 'e' || last == 'i' || last ==
'o' || last == 'u') {
[Link](cities[i]);
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
String[] cities = new String[10];
for (int i = 0; i < 10; i++) {
[Link]("Enter city name " + (i + 1) + ": ");
cities[i] = [Link]();
CityNames obj = new CityNames();
[Link](cities);
}
Q17
import [Link];
class BinarySearch {
void search(int[] arr, int key) {
int low = 0;
int high = [Link] - 1;
int pos = -1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == key) {
pos = mid;
break;
if (key < arr[mid]) {
high = mid - 1;
} else {
low = mid + 1;
if (pos != -1) {
[Link]("Number found at position: " + pos);
} else {
[Link]("Number is not in array");
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int[] arr = new int[10];
[Link]("Enter 10 sorted integers:");
for (int i = 0; i < 10; i++) {
arr[i] = [Link]();
[Link]("Enter number to search: ");
int key = [Link]();
BinarySearch obj = new BinarySearch();
[Link](arr, key);
}
Q18
class WordFilter {
void displayWords(String[] words) {
for (int i = 0; i < [Link]; i++) {
String w = words[i];
int len = [Link]();
char start = [Link]([Link](0));
char end = [Link]([Link](len - 1));
if (start == 'a' && end == 'a') {
[Link](w);
public static void main(String[] args) {
String[] words = {"Hari", "Anita", "Akash", "Amrita", "Alina",
"Devi", "Rishab", "John", "Farha", "AMITHA"};
WordFilter obj = new WordFilter();
[Link](words);
}
Q19
import [Link];
class AbbreviatedName {
void printAbbreviation(String name) {
String[] parts = [Link](" ");
String result = "";
for (int i = 0; i < [Link] - 1; i++) {
result = result + parts[i].charAt(0) + ". ";
result = result + parts[[Link] - 1];
[Link](result);
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
AbbreviatedName obj = new AbbreviatedName();
[Link]("Enter full name: ");
String name = [Link]();
[Link](name);
}
Q20
import [Link];
class BubbleSort {
void sort(int[] arr) {
for (int i = 0; i < [Link] - 1; i++) {
for (int j = 0; j < [Link] - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int t = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = t;
[Link]("Sorted Array:");
for (int i = 0; i < [Link]; i++) {
[Link](arr[i] + " ");
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int[] nums = new int[15];
[Link]("Enter 15 integers:");
for (int i = 0; i < 15; i++) {
nums[i] = [Link]();
BubbleSort obj = new BubbleSort();
[Link](nums);