Computer Project
Computer Project
I would like to express my greatest gratitude to all those who directly or indirectly helped
Firstly, I am highly indebted to my computer teacher Mr. Siddharth Mishra sir for his
I also acknowledge the endless contribution of my parents and friends who always
encouraged me and support me and their wistful cooperation in my studies and to complete
A Magic number is a number where the sum of the digits is recursively calculated until a
single digit is obtained, and if the single digit is 1, the number is considered a Magic number.
CODE:
import java.util.Scanner;
if (num == 0)
return 0;
if (isMagicNumber(number))
else
sc.close();
OUTPUT:
ALGORITHM:
Step 1: Start.
If the number is less than 10, check if it is 1. If yes, return true; otherwise, return
false.
Else, find the sum of the digits using the sumOfDigits() function.
Recursively check if the sum is a Magic number by calling isMagicNumber(sum).
Step 5: In the main method, get the input from the user.
Step 7: Print whether the number is a Magic number or not based on the result.
Step 8: End.
Write a Java program to find the HCF (Highest Common Factor) of two numbers using
recursion.
CODE:
import java.util.Scanner;
if (b == 0)
return a;
System.out.println("The HCF of " + num1 + " and " + num2 + " is: " + hcf);
sc.close();
OUTPUT:
ALGORITHM:
Step 1: Start.
Step 7: End.
Write a program in Java to check if a given number is an Armstrong number using recursion .
CODE:
import java.util.Scanner;
if (num == 0)
return 0;
if (num == 0)
return 0;
if (isArmstrong(num))
else
sc.close();
OUTPUT:
ALGORITHM:
Step 1: Start
Step 3: Define a function countDigits to count the number of digits in the given number:
Step 4: Define a recursive function armstrongSum to calculate the sum of digits raised to
the power of the total number of digits:
Step 5: In the main function, call isArmstrong to check if the number equals the result of
armstrongSum.
Step 8: End
Write a Java program to convert a decimal number to its binary equivalent using recursion.
CODE:
import java.util.Scanner;
if (num == 0)
return "";
if (decimal == 0)
else
sc.close();
OUTPUT:
ALGORITHM:
Step 2: Base Case: If the decimal number is 0, return an empty string (end of recursion).
Write a Java program to generate the Fibonacci series using recursion. The program should
display the first n numbers in the Fibonacci sequence, where n is provided by the user.
CODE:
import java.util.Scanner;
if (n == 0)
return 0;
else if (n == 1)
return 1;
else
int n = sc.nextInt();
System.out.println("Fibonacci series:");
OUTPUT:
ALGORITHM:
Step 1: Start
If n == 0, return 0.
If n == 1, return 1.
Otherwise, return fibonacci(n-1) + fibonacci(n-2).
Step 4: End
CODE:
import java.util.Scanner;
if (n == 0 || n == 1)
else
sc.close();
OUTPUT:
ALGORITHM:
Step 1: Start
Step 4: End
Variable Description Chart (VDC):
Write a Java program to arrange elements of each row of a 2D array in ascending order
using Bubble Sort. The program should take input for a 2D array, and for each row, it should
sort the elements in ascending order using the Bubble Sort algorithm.
CODE:
import java.util.Scanner;
int n = row.length;
row[j + 1] = temp;
}
}
System.out.println();
array[i][j] = sc.nextInt();
sortRows(array);
displayArray(array);
}
OUTPUT:
ALGORITHM:
Step 6: End.
Write a program in Java to check if a given 2D array is symmetric or not. A 2D array is said
to be symmetric if it is equal to its transpose (i.e., the element at position [i][j] is the same
as the element at [j][i] for all i and j).
CODE:
import java.util.Scanner;
if (matrix[i][j] != matrix[j][i])
return false; // If any element doesn't match its transpose, it's not
symmetric
return true;
int n = sc.nextInt();
matrix[i][j] = sc.nextInt();
if (isSymmetric(matrix, n))
else
{
sc.close();
OUTPUT:
ALGORITHM:
Step 1: Start.
Step 2: Declare the method isSymmetric which takes the matrix and its size as parameters.
Step 3: In the method, iterate through the matrix and check if matrix[i][j] is equal to
matrix[j][i] for all i and j.
If any pair of elements don't match, return false (matrix is not symmetric).
If all elements match, return true (matrix is symmetric).
Step 7: Print the result based on the return value of the method.
Step 8: End.
Variable Description Chart (VDC):
Write a Java program to find the product of two matrices of size m x n and n x p. The
program should take input for both matrices and display the resultant matrix. Ensure that
the number of columns in the first matrix is equal to the number of rows in the second
matrix.
CODE:
import java.util.Scanner;
public static int[][] multiplyMatrices(int[][] matrix1, int[][] matrix2, int rows1, int cols1,
int cols2)
result[i][j] = 0;
}
return result;
System.out.print(matrix[i][j] + "\t");
System.out.println();
if (cols1 != rows2)
return;
matrix1[i][j] = sc.nextInt();
matrix2[i][j] = sc.nextInt();
sc.close();
OUTPUT:
ALGORITHM:
Step 3: Check if matrix multiplication is possible by verifying that the number of columns of
the first matrix equals the number of rows of the second matrix.
Multiply corresponding elements of the first and second matrix, and accumulate the
sum.
Write a Java program to arrange all the elements of each row of an n * n matrix in ascending
order using Bubble Sort. The program should sort each row individually and print the sorted
matrix.
CODE:
import java.util.Scanner;
int n = arr.length;
arr[j + 1] = temp;
}
public static void main(String[] args)
int n = sc.nextInt();
// Declare matrix
matrix[i][j] = sc.nextInt();
bubbleSortRow(matrix[i]);
}
// Display sorted matrix
System.out.print(matrix[i][j] + "\t");
System.out.println();
sc.close();
OUTPUT:
ALGORITHM:
Step 4: For each row of the matrix, apply the Bubble Sort algorithm:
Loop through the row and compare each adjacent pair of elements.
If an element is greater than the next one, swap them.
Repeat the process for all elements in the row until the row is sorted.
Write a program to input a name and print the initials of that name except the last word
(surname), each initial must be followed by symbol dot ().
Example :
Input : Mohandas Karamchand Gandhi
Output : M. K. Gandhi
CODE:
import java.util.Scanner;
initials.append(words[i].charAt(0)).append(". ");
// Output result
System.out.println(initials.toString() + surname);
scanner.close();
OUTPUT:
ALGORITHM:
Step 1: Start.
Step 6: Split the name into an array of words using space as the delimiter.
Step 7: Retrieve the last word as the surname.
Extract the first character and append it to the initials followed by a dot.
Write a program to input 2 words from the user. The first character of the first word is
followed by the first character of the second word and so on. If the words are of different
length, the remaining characters of the longer word are put at the end.
Example: If the first word is “JUMP” and the second word is “STROLL”, then the required
word will be “JSUTMRPOLL”
CODE:
import java.util.Scanner;
scanner.close();
mixed.append(word1.charAt(i));
mixed.append(word2.charAt(i));
mixed.append(word1.substring(minLength));
else
{
mixed.append(word2.substring(minLength));
return mixed.toString();
OUTPUT:
ALGORITHM:
Step 4: Iterate through the characters of both words up to the length of the shorter word:
Write a Java program to check whether a given string is a palindrome or not using recursion.
CODE:
import java.util.Scanner;
return true;
if (str.charAt(start) != str.charAt(end))
// Recursive call
{
Scanner scanner = new Scanner(System.in);
if (result)
else
scanner.close();
OUTPUT:
ALGORITHM:
Step 2: Clean the input string by removing spaces and converting it to lowercase.
Step 3: Define a recursive method isPalindrome that takes three parameters: the string,
the starting index, and the ending index.
Base Case: If the starting index is greater than or equal to the ending index, return
true.
Check Characters: Compare the characters at the starting and ending indices. If they
are not equal, return false.
Recursive Case: Call the isPalindrome method with the next starting index and previous
ending index.
Step 4: Display the result indicating whether the original string is a palindrome.
Write a Java program to delete multiple specified characters from a given sentence. The
program should take a sentence and a string of characters to be deleted, and it should
output the modified sentence.
CODE:
import java.util.Scanner;
if (ch == delCh)
toDelete = true;
break;
result.append(ch);
return result.toString();
OUTPUT:
ALGORITHM:
Step 1: Input: Read the original sentence and the characters to be deleted from the user.
Step 2: Initialization: Convert the string of characters to be deleted into a character array
for easy processing.
Step 3: Processing:
Step 4: Output: Convert the StringBuilder to a string and print the modified sentence.
Variable Description Chart (VDC):
Write a Java program to replace all vowels in a given string with the letter that succeeds
them in the alphabet. The vowels are 'a', 'e', 'i', 'o', and 'u', and their successors are 'b',
'f', 'j', 'p', and 'v' respectively. The program should handle both uppercase and lowercase
vowels.
CODE:
import java.util.Scanner;
char ch = input.charAt(i);
switch (ch)
return result.toString();
sc.close();
}
OUTPUT:
ALGORITHM:
Step 4: Output: After processing all characters, print the modified string.
Write a program in Java to calculate a telephone bill using the concept of inheritance. The
program should contain a base class with customer details and a derived class that calculates
the bill based on the number of calls made. Assume the following rates:
CODE:
import java.util.Scanner;
class Customer
String customerName;
String phoneNumber;
this.customerName = customerName;
this.phoneNumber = phoneNumber;
void displayCustomerDetails()
int numberOfCalls;
double billAmount;
this.numberOfCalls = numberOfCalls;
this.billAmount = 0.0;
void calculateBill()
billAmount = (numberOfCalls - 100) * 1.0; // ₹1 per call for the next 200 calls
}
else
billAmount = (200 * 1.0) + (numberOfCalls - 300) * 1.5; // ₹1.50 for calls above 300
void displayBill()
bill.calculateBill();
bill.displayBill();
sc.close();
OUTPUT:
ALGORITHM:
Step 2: Define a base class Customer that contains customer details (name and phone
number) and a method to display these details.
Step 4: In TelephoneBill, accept the number of calls and calculate the bill based on the rate:
Step 5: Display the customer details along with the number of calls and the total bill
amount.
Step 6: In the main() method, accept input for the customer's name, phone number, and
number of calls.
Step 7: Create an object of the TelephoneBill class and call its methods to calculate and
display the bill.
Write a Java program to find the radius and area of a circle using the concept of
inheritance.
CODE:
class Circle
public Circle(double r)
radius = r;
public CircleArea(double r)
{
super(r);
}
OUTPUT:
ALGORITHM:
Step 1: Start
Step 2: Define a base class Circle with a protected variable radius and a method
displayRadius() to display the radius.
Step 4: In CircleArea, define a method displayArea() to calculate and display the area of
the circle using the formula:
𝑨𝒓𝒆𝒂 = 𝝅 ∗ 𝒓𝒂𝒅𝒊𝒖𝒔𝟐
Step 5: In the main() method, create an object of CircleArea class, passing the radius value
to the constructor.
Step 8: End
Write a Java program to demonstrate single inheritance where a Person class is extended by
a Student class. The Person class should have attributes like name and age, while the
Student class should have attributes like roll number and marks. The program should display
the details of a student.
CODE:
// Class Person
class Person
String name;
int age;
this.name = name;
this.age = age;
int rollNumber;
double marks;
super(name, age);
this.rollNumber = rollNumber;
this.marks = marks;
displayPerson();
student.displayStudent();
OUTPUT:
ALGORITHM:
CODE:
import java.util.Scanner;
class Rectangle
Rectangle(int l, int w)
length = l;
width = w;
int calculateArea()
void compareArea(Rectangle r)
else
rect1.compareArea(rect2);
sc.close();
}
OUTPUT:
ALGORITHM:
Step 1: Start.
Step 2: Create a Rectangle class with attributes length and width and the following
methods:
Prompt the user to enter the length and width of the first rectangle.
Create a Rectangle object rect1 with the given dimensions.
Prompt the user to enter the length and width of the second rectangle.
Create a Rectangle object rect2 with the given dimensions.
Calculate and display the areas of rect1 and rect2 using the calculateArea() method.
Compare the areas of rect1 and rect2 using the compareArea() method.
Step 4: End.
Variable Description Chart (VDC):
Write a Java program to display the calendar of a month by accepting the first day of the
month (as an integer from 0 for Sunday to 6 for Saturday) and the total number of days in
the month. The program should print the calendar in a tabular format, where each week
starts on Sunday.
CODE:
import java.util.Scanner;
System.out.print("Enter the first day of the month (0 for Sunday, 1 for Monday, ... 6
for Saturday): ");
// Display calendar
System.out.print(" ");
if ((day + firstDay) % 7 == 0)
System.out.println();
OUTPUT:
ALGORITHM:
Step 1: Start.
Step 2: Accept the first day of the month as an integer (0 for Sunday, 1 for Monday, ..., 6
for Saturday).
Step 4: Print the headers for the days of the week: "Sun Mon Tue Wed Thu Fri Sat".
Step 5: Use a loop to print the necessary leading spaces based on the first day of the
month.
Step 6: Start printing the days of the month from 1 up to the number of days entered.
Step 7: After every 7th day (i.e., after Saturday), move to the next line.
Step 8: End the loop once all days of the month are printed.
Step 9: End.
Write a Java program to insert elements at the rear end of a linked list.
CODE:
import java.util.Scanner;
class Node
int data;
Node next;
this.data = data;
this.next = null;
class LinkedList
Node head;
public LinkedList()
head = null;
{
Node newNode = new Node(data);
if (head == null)
head = newNode;
else
temp = temp.next;
temp.next = newNode;
if (head == null)
else
temp = temp.next;
System.out.println("null");
int n = sc.nextInt();
list.insertAtRear(element);
}
System.out.println("Linked list after insertion:");
list.display();
OUTPUT:
ALGORITHM:
Step 1: Start
Step 2: Create a class Node with an integer data and a Node reference next.
Step 3: Create a class LinkedList that has a head node initialized to null.
Step 5: Define the display method to print all elements of the list.
Step 6: In the main method, prompt the user to enter the number of elements to insert.
Step 7: For each element, call the insertAtRear method to add the element to the list.
Step 8: Call the display method to print the elements of the linked list.
Step 9: End
Variable Description Chart (VDC):
Write a Java program to delete an element at the rear end of a linked list. The program
should implement a linked list with basic functionalities, including deletion from the rear end.
Display the list before and after deletion.
CODE:
import java.util.Scanner;
class Node
int data;
Node next;
this.data = data;
this.next = null;
class LinkedList
Node head;
public LinkedList()
head = null;
}
if (head == null)
head = newNode;
else
temp = temp.next;
temp.next = newNode;
if (head == null)
if (head.next == null)
head = null;
else
temp = temp.next;
temp.next = null;
if (head == null)
System.out.println("List is empty.");
else
{
temp = temp.next;
System.out.println();
int n = sc.nextInt();
list.insertAtRear(element);
System.out.println("\nBefore deletion:");
list.display();
list.deleteAtRear();
System.out.println("\nAfter deletion:");
list.display();
sc.close();
OUTPUT:
ALGORITHM:
Step 1: Node class: Define a Node class to represent each node of the linked list, which
contains two fields: data and next.
Initialize the linked list and accept the number of elements from the user.
Insert the elements in the list.
Display the list before and after deletion of the rear-end element.
Write a Java program to display the upper triangular matrix of a given square matrix. The
upper triangular matrix contains elements above and on the main diagonal, while all elements
below the main diagonal are set to zero.
CODE:
import java.util.Scanner;
int n = sc.nextInt();
matrix[i][j] = sc.nextInt();
}
System.out.println("Original Matrix:");
System.out.println();
if (i > j)
System.out.print("0 ");
else
System.out.println();
sc.close();
OUTPUT:
ALGORITHM:
Initialize an n x n matrix.
If the row index i is greater than the column index j, set the element to 0 (because
it's below the diagonal).
Write a Java program to sort two arrays using the insertion sort algorithm. The program
should take two arrays as input and sort them in ascending order using the insertion sort
technique.
CODE:
import java.util.Scanner;
int n = arr.length;
int j = i - 1;
arr[j + 1] = arr[j];
j = j - 1;
arr[j + 1] = key;
}
}
arr1[i] = sc.nextInt();
arr2[i] = sc.nextInt();
insertionSort(arr1);
insertionSort(arr2);
OUTPUT:
ALGORITHM:
Accept two arrays from the user, including their sizes and elements.
For each element in the array (starting from the second element):
Write a Java program to check whether two given numbers are amicable numbers or not.
Amicable numbers are two numbers such that the sum of the proper divisors (excluding the
number itself) of one number is equal to the other number and vice versa.
CODE:
import java.util.Scanner;
int sum = 0;
if (num % i == 0)
sum += i;
return sum;
else
System.out.println(num1 + " and " + num2 + " are not amicable numbers.");
sc.close();
}
OUTPUT:
ALGORITHM:
Step 1: Input: Accept two numbers (num1 and num2) from the user.
BOOKS: 1. Computer Science with Java class XII ISC – Sumita Arora .
WEBSITES: 1. https://openai.com/
2. https://www.knowledgeboat.com/
3. https://www.scribd.com/
4. https://simplycoding.in/