Name - Aryan Mishra Class-12 A SL - NO-07 ROLL - NO-4403 Subject - Computer
Name - Aryan Mishra Class-12 A SL - NO-07 ROLL - NO-4403 Subject - Computer
CLASS- 12 A
SL.NO-07
ROLL.NO-4403
SUBJECT- COMPUTER
1
ACKNOWLEDGEMENT
I would like to extend my heartfelt gratitude to everyone
who played a role in the successful completion of this
project. Their guidance, encouragement, and expertise
were invaluable, and I am truly thankful for their support.
1.Our Computer Science Teacher (Spondon
Ganguly Sir): Thank you for imparting your
knowledge, patiently answering our questions, and
providing valuable insights throughout the project.
2.Classmates and Friends: Your brainstorming
sessions, late-night coding marathons, and
collaborative efforts made this project both enjoyable
and enlightening.
3.Families: Their unwavering encouragement and
understanding during those intense project weeks
kept us motivated.
-Aryan Mishra
2
INDEX
Page Content
Sl.n No.
o
1. 4 Introduction To java
2. 5 Concept of oops
4. 7-10 Question 1
5. 11-15 Question 2
6. 16-20 Question 3
7. 21-25 Question 4
8. 26-29 Question 5
9. 30-33 Question 6
10. 34-38 Question 7
11. 39-41 Question 8
12. 42-45 Question 9
13. 46-48 Question 10
14. 49-51 Question 11
15. 52-54 Question 12
16. 55-57 Question 13
17. 58-60 Question 14
18. 61-64 Question 15
19. 65 Bibliography
3
JAVA-AN INTRODUCTION
5
CONCEPT OF OBJECT ORIENTED
PROGRAMMING
Object-oriented programming (OOP) is a powerful
paradigm that revolutionizes the way we approach
software development. By organizing code around
objects—collections of data and methods that
operate on that data—OOP enhances modularity,
reusability, and maintainability. The core principles
of OOP include encapsulation, inheritance, and
polymorphism, which together allow developers to
create more intuitive and manageable code
structures.
7
Question 1.
Algorithm:
8
Program Code:
import java.util.*;
if (digitSeen[digit]) {
return false;
digitSeen[digit] = true;
currentNumber /= 10;
return true;
int count = 0;
9
System.out.println("Unique digit integers between " +
m + " and " + n + ":");
if (hasUniqueDigits(number)) {
System.out.println(number);
count++;
int m = scanner.nextInt();
int n = scanner.nextInt();
// Ensure m < n
if (m >30000 || n>30000) {
10
System.out.println("Invalid input: Value should be
less than 30000");
return;
m=m+n;
n=m-n;
m=m-n;
findUniqueDigitIntegers(m, n);
scanner.close();
11
processing a
number.
Variable to hold
the current
currentNumber int
number being
checked.
Variable to hold
the current digit of
digit int
the number being
checked.
Variable used in
the loop to iterate
number int
over the range
from m to n.
Output:
12
Question 2.
Write a program which takes a string (maximum 80
characters) terminated by a full stop. The
words in this string are assumed to be separated by one or
more blanks.
Arrange the words of the input string in descending order of
their lengths. Same-length words
should be sorted alphabetically. Each word must start with
an uppercase letter and the sentence
should be terminated by a full stop.
INPUT:
“This is human resource department.”
OUTPUT:
Department Resource Human This Is.
Algotrithm:
· Input Reading:
· Validation:
· String Processing:
· String Reconstruction:
· Output:
13
Program Code:
import java.util.Scanner;
words[i] = capitalizeFirstLetter(words[i]);
sortWords(words);
14
return joinWords(words);
return word;
words[i] = words[j];
words[j] = temp;
15
}
String newStr="";
newStr+=words[i]+" ";
if(input.length()>80){
System.out.println("Maximum 80 characters
allowed!! Try Again!");
16
main();
if(input.charAt(input.length()-1)!='.'){
main();
printOutput(input,output);
17
Processed output
output String string after sorting
words.
Temporary
variable used for
temp String
swapping words
during sorting.
Output:
18
Question 3. Write a program that accepts N words from the
user, where N will also be entered by the user.
Now print all those words which are anagrams among
themselves. If no anagram is possible, the
program will display an appropriate message. The program
should take words containing only
alphabets (in upper or lower or mixed case).
Also, display the number of anagrams possible.
Algortihm:
· Input Reading:
· Anagram Finding:
· Output:
· Anagram Check:
19
If the frequency counts match for all letters, the words
are anagrams.
Program Code:
import java.util.Scanner;
int n = sc.nextInt();
words[i] = sc.nextLine();
20
findAndPrintAnagrams(words);
sc.close();
if (areAnagrams(words[i], words[j])) {
if (!isAnagramGroup) {
isAnagramGroup = true;
foundAnagram = true;
21
if (isAnagramGroup) {
System.out.println();
anagramCount++;
if (!foundAnagram) {
} else {
word1 = word1.toLowerCase();
word2 = word2.toLowerCase();
if (word1.length() != word2.length()) {
return false;
22
int[] letterCounts = new int[26]; // Array to count letter
frequencies
letterCounts[word1.charAt(i) - 'a']++;
letterCounts[word2.charAt(i) - 'a']--;
if (count != 0) {
return false;
return true;
23
starts a new
anagram group.
First word in the
word1 String pair being checked
for anagrams.
Second word in the
word2 String pair being checked
for anagrams.
Array to count the
frequency of
letterCounts int[]
letters in the
words.
Temporary
variable to hold
count int the letter count
during anagram
check.
Output:
Question 4.
Numbers have different representations depending on the
bases on which they are expressed. For example, 110 in
base 3 = 12 (1x3² + 1x3¹ +0x3º), also 14 in base 8 = 12 (1x8¹
+ 4x8º). So, we can say that 110 and 14 will generate the
same value i.e., 12 in the bases 3 and 8 respectively.
Consider, for example, the integers 12 and 5. Certainly,
these are not equal if base 10 is used for each. But suppose
12 was a base 3 number and 5 was a base 6 number then
what happens, 12 base 3=1x3¹ + 2x3º = 5, and 5 in base
6=5x60 = 5 or 5 base 10 (5 in any base is equal to 5 base
10). So, 12 and 5 can be equal of at the bases 3 and 6
respectively.
Write a program to input two integers, X and Y, and
calculate the smallest base for X and the smallest base for Y
(likely different from X) so that X and Y represent the same
value. The base associated with x and Y will be between 1
and 20 (both inclusive). In representing these numbers, the
digits 0 and 9 have their usual decimal interpretation. The
24
uppercase characters A to J represent digits 10 to 19
respectively
Algorithm:
· Input Handling:
25
Convert the string Y from the current base to its
decimal equivalent using the same conversion method.
If the decimal values of X and Y match for any pair of
bases, print the bases and the matching decimal value,
and terminate the program.
· No Match Found:
26
Program Code:
import java.util.Scanner;
if (valueX == valueY) {
System.out.println("Smallest base for X: " +
baseX);
System.out.println("Smallest base for Y: " +
baseY);
System.out.println("Value: " + valueX);
return;
}
}
}
27
if (Character.isDigit(ch)) {
minBase = Math.max(minBase,
Character.getNumericValue(ch) + 1);
} else {
minBase = Math.max(minBase, ch - 'A' + 11);
}
}
return new int[]{minBase, maxBase};
}
28
second integer,
converted to
uppercase.
Array containing
the minimum and
xBaseRange int[] maximum possible
bases for the
number X.
Array containing
the minimum and
yBaseRange int[] maximum possible
bases for the
number Y.
Variable used to
iterate through
baseX int
possible bases for
the number X.
Variable used to
iterate through
baseY int
possible bases for
the number Y.
Decimal value of
the number X
valueX int when converted
from the base
baseX.
Decimal value of
the number Y
valueY int when converted
from the base
baseY.
Input string
representing
number String
either X or Y in the
helper methods.
Minimum base
required for the
minBase int input number,
calculated based
on its characters.
Maximum base
limit for the input
maxBase int
number, arbitrarily
set to 20.
Character from the
input string used
ch char in base range and
decimal conversion
calculations.
29
Numerical value of
the character ch
digitValue int
when converted to
a digit.
Decimal value of
the input string
value int being converted in
the helper
methods.
Output:
30
Question 5.
Algortihm:
· Input Handling:
· Reading Input:
31
o If moveLeft is true and left is non-negative, place
the current element from the sorted array at the
left index of result and decrement left.
o If moveLeft is false and right is less than n, place
the current element from the sorted array at the
right index of result and increment right.
o Toggle moveLeft to alternate the placement
between left and right.
Program Code:
import java.util.*;
rearrangeArray(data);
System.out.println("Rearranged array:");
for (int i = 0; i < n; i++) {
System.out.print(data[i] + " ");
}
}
32
// Sort the array in descending order
Arrays.sort(data);
for (int i = 0; i < n / 2; i++) {
int temp = data[i];
data[i] = data[n - i - 1];
data[n - i - 1] = temp;
}
33
equal to 50.
Array of integers
data int[] entered by the
user.
Array used to store
result int[] the rearranged
elements.
Index representing
center int the center of the
result array.
Pointer used to
place elements to
left int the left of the
center in the result
array.
Pointer used to
place elements to
right int the right of the
center in the result
array.
Index used to
index int iterate through the
sorted data array.
Boolean flag used
to alternate
placement of
moveLeft boolean
elements between
left and right of
the center.
Temporary
variable used for
swapping
temp int
elements during
the reverse
operation.
Loop variable used
i int for various
iterations.
Output:
34
35
Question 6.
A palindrome number is one that reads the same from left to
right or right to left. There is an ingenious method to get a
palindrome from any positive integer. The procedure is as
follows: Start with any positive number of 2 or more digits.
Obtain another positive number by reversing the digits of
the starting number. Add the two numbers together. Repeat
the whole procedure with the sum as the starting number
till you get a sum that is a palindrome number. Program to
calculate the palindrome of a given number in a maximum
15 terms. Assume that the starting number is not a
palindrome. Sample Input: 87. Output: 4884 Steps: 4.
Algorithm:
· Input Handling:
· Calculate Palindrome:
· Initialize steps to 0.
Initialize currentNumber to startingNumber.
· Return Steps:
36
Program Code:
import java.util.Scanner;
/**
* Function to calculate the palindrome number derived
from a starting number.
* @param startingNumber The starting positive integer.
* @return The number of steps taken to find the
palindrome.
*/
private static int calculatePalindrome(int startingNumber)
{
int steps = 0;
int currentNumber = startingNumber;
37
// Update current number to the sum for the next
iteration
currentNumber = sum;
steps++;
}
/**
* Function to reverse the digits of a given number.
* @param number The number to reverse.
* @return The reversed number.
*/
private static int reverseNumber(int number) {
int reversed = 0;
while (number > 0) {
reversed = reversed * 10 + number % 10;
number /= 10;
}
return reversed;
}
/**
* Function to check if a number is palindrome.
* @param number The number to check.
* @return True if the number is palindrome, false
otherwise.
*/
private static boolean isPalindrome(int number) {
int original = number;
int reversed = 0;
38
Variable Name Type Description
Scanner object
used for reading
scanner Scanner
input from the
user.
The starting
number entered by
startingNumber int
the user, which is
greater than 9.
Counter for the
number of steps
steps int
taken to find the
palindrome.
The current
number being
currentNumber int
processed to find
the palindrome.
The reversed
reversedNumber int digits of the
current number.
The sum of
currentNumber
sum int
and
reversedNumber.
The original
number being
checked if it is a
original int
palindrome (used
in isPalindrome
function).
The reversed
digits of the
original number
reversed int
(used in
isPalindrome
function).
Temporary
variable used for
number int
reversing the
digits of a number.
Output:
39
40
Question 7.
Write a program to find the age of a person in year-month-
day after taking the birth date and current date as input in
dd-mm-yyyy format. The required validations of the
existence of the dates must be given in the program.
Algorithm:
· Input Handling:
· Validation:
· Date Comparison:
· Age Calculation:
41
Program Code:
import java.util.Scanner;
// Split the birth date input into day, month, and year
String[] birthDateParts = birthDateInput.split("-");
String[] currentDateParts = currentDateInput.split("-");
// Split the current date input into day, month, and year
int currentDay = Integer.parseInt(currentDateParts[0]);
int currentMonth =
Integer.parseInt(currentDateParts[1]);
int currentYear =
Integer.parseInt(currentDateParts[2]);
42
int[] age = calculateAge(birthDay, birthMonth,
birthYear, currentDay, currentMonth, currentYear);
System.out.println("Age: " + age[0] + " years, " +
age[1] + " months, " + age[2] + " days.");
} else {
System.out.println("Birth date should be before the
current date.");
}
}
43
if (year1 == year2) {
if (month1 < month2) {
return true;
}
if (month1 == month2) {
return day1 < day2;
}
}
return false;
}
return age;
}
}
44
representing the
birth date in dd-
mm-yyyy format.
Input string
representing the
currentDateInput String
current date in dd-
mm-yyyy format.
Array containing
birthDateParts String[] parts of the birth
date split by -.
Array containing
parts of the
currentDateParts String[]
current date split
by -.
Day part of the
birthDay int
birth date.
Month part of the
birthMonth int
birth date.
Year part of the
birthYear int
birth date.
Day part of the
currentDay int
current date.
Month part of the
currentMonth int
current date.
Year part of the
currentYear int
current date.
Array containing
the calculated age
age int[]
in years, months,
and days.
Temporary
variable
day int representing day
in isValidDate
function.
Temporary
variable
representing
month int
month in
isValidDate
function.
Temporary
variable
year int representing year
in isValidDate
function.
days int[] Array containing
the number of
days in each
45
month.
Original year in
original int isLeapYear
function.
Reversed digits of
the original year in
reversed int
isLeapYear
function.
Date components
of the first date
day1, month1,
int (birth date) in
year1
isDateBefore
function.
Date components
of the second date
day2, month2,
int (current date) in
year2
isDateBefore
function.
Output:
46
Question 8.
Given a time in numbers we can convert it into words. For
example, 5: 00 five o'clock 5: 10 ten minutes past five 5: 15
quarter past five 5: 30 half past five 5: 40 twenty minutes to
six 5: 45 quarter to six 5: 47 thirteen minutes to six Write a
program which first inputs two integers, the first between 1
and 12 (both inclusive) and the second between 0 and 59
(both inclusive) and then prints out the time they represent,
in words. Your program should follow the format of the
examples above.
Algorithm:
· Input:
· Validation:
· Conversion to Words:
· Output:
Program Code:
47
import java.util.Scanner;
/**
* Convert the given hour and minutes into words.
* @param hour The hour of the time (1-12).
* @param minutes The minutes of the time (0-59).
* @return The time represented in words.
*/
private static String convertTimeToWords(int hour, int
minutes) {
// Array of words for numbers from 0 to 29
String[] numbers = {
"zero", "one", "two", "three", "four", "five", "six",
"seven", "eight", "nine", "ten",
"eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen", "nineteen", "twenty",
"twenty one", "twenty two", "twenty three", "twenty
four", "twenty five", "twenty six", "twenty seven", "twenty
eight", "twenty nine"
};
48
// Convert the time based on the value of minutes
if (minutes == 0) {
return numbers[hour] + " o'clock";
} else if (minutes == 15) {
return "quarter past " + numbers[hour];
} else if (minutes == 30) {
return "half past " + numbers[hour];
} else if (minutes == 45) {
return "quarter to " + numbers[(hour % 12) + 1];
} else if (minutes == 1) {
return "one minute past " + numbers[hour];
} else if (minutes < 30) {
return numbers[minutes] + " minutes past " +
numbers[hour];
} else if (minutes > 30 && minutes < 60) {
return numbers[60 - minutes] + " minutes to " +
numbers[(hour % 12) + 1];
}
return "";
}
}
Output:
49
50
Question 9.
Consider the sequence of natural numbers 1, 2, 3, 4, 5, 6, 7
…….. Removing every second number produces the
sequence 1, 3, 5, 7, 9, 11, 13, 15, 17 ……… Removing every
third number from above produces the sequence 1, 3, 7, 9,
13, 15, 19, 21, 25, 27 ………. This process continues
indefinitely by removing the fourth, fifth…........ and so on,
till after a fixed number of steps, certain natural numbers
remain indefinitely. These are known as Lucky Numbers.
Write a program to generate and print lucky numbers less
than a given natural number N where N<=50.
Algorithm:
· Input Reading
· Input Validation
· Initialization
· Initialize counter to 2.
Enter a loop that continues until the number of
remaining "lucky" numbers is less than counter:
51
o Increment Counter
· Output
Program Code:
import java.util.Scanner;
52
// If fewer numbers remain than the current counter,
break the loop
if (remaining < counter) {
break;
}
53
removing numbers
from the lucky list
(starts at 2 and
increments).
Count of numbers
remaining int that are still
considered lucky.
Counter to keep
track of numbers
removed int
removed in each
iteration.
Output:
54
Question 10. Given a sequence of N integers 1<=N<=30. A
maximal subsequence (for the purpose of the program) is
the subsequence with the highest sum. Thus, for the
sequence given below: -1, 2, 8, -7, 3, 5, -20, 2, 1. The
maximal subsequence runs from the 2nd number to the 6th
number (the sum of the numbers is 11), since no other
subsequence has the highest sum. (Note: To get a
subsequence the position of the elements in the sequence
should not be altered.) Write a program which reads the
sequence and finds its maximal subsequence. Input will
consist of a number (N) and followed by N numbers. The
sequence will have a positive sum for the maximal
subsequence. Output should consist of the start and the end
indices of the maximal subsequence and its resulting sum. If
more than one maximal subsequence is present then print
any one of them.
Algorithm:
· Input Reading
· Input Validation
· Initialization
· Kadane's Algorithm
55
o If currentSum becomes negative, reset
currentSum to 0 and update tempStartIndex to
the next index.
· Output
Program Code:
import java.util.Scanner;
56
maxSum = currentSum;
startIndex = tempStartIndex;
endIndex = i;
}
if (currentSum < 0) {
currentSum = 0;
tempStartIndex = i + 1;
}
}
57
Output:
58
Question 11. Trinomial Triangle The trinomial triangle is a
number triangle of trinomial coefficients. It can be obtained
by starting with a row containing a single "1" and the next
row containing three 1s and then letting subsequent row
elements be computed by summing the elements above to
the left, directly above, and above to the right: Write a
program to accept the number of terms from the user and
print the above pattern of the Trinomial triangle in the given
format. Use the recursive technique to print the pattern. 1 1
1 1 1 2 3 2 1 1 3 6 7 6 3 1 1 4 10 16 19 16 10 4 1
Algorithm:
· Input Reading
· Triangle Generation
· For each row from 0 to n-1 (let's call the current row
index i):
o If n is 0 and k is 0, return 1.
o If k is less than -n or greater than n, return 0.
o Otherwise, return the sum of trinomialValue(n - 1,
k - 1), trinomialValue(n - 1, k), and
trinomialValue(n - 1, k + 1).
59
Program Code:
import java.util.Scanner;
60
trinomial values
loop.
Function to
compute the
trinomialValue int
trinomial value at
a given (n, k).
Current column
index relative to
k int the current row in
the trinomial value
calculation.
Output:
61
Question 12. Unique Prime Number A prime number whose
all the digits are unique and not repeated in the number. For
example, 3, 7, 13, 37… whereas 11, 101… are not unique
prime numbers. Write a program in Java to accept a positive
number and check and print if it is a Unique Prime number
or a non-unique prime number. Use a separate method for
prime check that will use the recursive technique to check
the number passed as the argument is prime or not and
return true or false.
Algorithm:
· Input Reading
o Inside isUniquePrime:
Check if the number is prime using the
isPrime method.
Check if the number has unique digits using
the hasUniqueDigits method.
Return true if both conditions are met,
otherwise return false.
· Prime Check
62
o Initialize a boolean array digitSeen to keep track
of digits from 0 to 9.
o While there are digits left in the number:
· Output Result
Program Code:
import java.util.Scanner;
if (isUniquePrime(number)) {
System.out.println(number + " is a unique prime
number.");
} else {
System.out.println(number + " is a non-unique prime
number.");
}
}
63
return false; // Numbers less than or equal to 1 are
not prime
}
if (divisor == 1) {
return true; // Base case: if divisor is 1, the number is
prime
}
if (number % divisor == 0) {
return false; // If number is divisible by any divisor, it
is not prime
}
return isPrime(number, divisor - 1); // Recursive call
with decremented divisor
}
64
used to check if
the number is
prime.
Result indicating
whether the
isPrime boolean
number is prime or
not.
Result indicating
whether all digits
hasUniqueDigits boolean
in the number are
unique.
Output:
65
Question 13. Parkside's Triangle is a mathematical pattern
that generates a triangle of numbers given two variables,
the size and the seed. Parkside's triangle is generated from
two numbers: the number of rows R, 1 <= R <= 39, and the
seed S, 1 <= S <= 9. The seed determines the upper left
number of the triangle. Using the sample input (below) to
determine the pattern of Parkside's Triangle, write a
program that accepts R and S from the input file and prints
Parkside's Triangle for those two values. INPUT FORMAT:
Two integers, R and S. OUTPUT FORMAT: Parkside's triangle,
printed with no extra blanks on the end and with the proper
spacing in the front as shown in the sample. INPUT: 6 1
OUTPUT : 1 2 4 7 2 7 3 5 8 3 8 6 9 4 9 1 5 1 6 2 3
Algortihm:
· Input Reading:
· Initialize Triangle:
· Print Triangle:
66
· For each row iii:
Program Code:
import java.util.Scanner;
67
triangle[i][j]-=9;
}
c++;
cc=c;
}
68
elements within a
row.
Loop variable for
k int printing leading
spaces.
Output:
69
Question 14. Write a program to read three lines of upper
case (i.e., all CAPITAL LETTERS) text input (not more than 72
characters per line) from the user and print a vertical
histogram that shows how many times each letter (but not
blanks, digits, or punctuation) appears in the all-upper-case
input. Format your output exactly as shown below. INPUT
FORMAT: Three lines of upper-case text, not more than 72
characters per line. OUTPUT FORMAT: Several lines with
asterisks and spaces are followed by one line with the
upper-case alphabet separated by spaces. Do not print
unneeded blanks at the end of any line. Do not print any
leading blank lines.
Algorithm:
· Initialize:
· Read Input:
· Process Input:
· Print Histogram:
70
Program Code:
import java.util.Scanner;
71
System.out.println();
}
System.out.println();
}
}
Output:
72
73
Question 15. The computer department of the Agency of
International Espionage is trying to decode intercepted
messages. The agency’s spies have determined that the
enemy encodes messages by first converting all characters
to their ASCII values and then reversing the string. For
example, consider A_z (the underscore is just to highlight
the space). The ASCII values for A, , z are 65, 32, 122
respectively. Concatenate them to get 6532122, and then
reverse this to get 2212356 as the coded message. Write a
program which reads a coded message and decodes it. The
coded message will not exceed 200 characters. it will
contain only alphabets (A……Z, and a-z) and spaces. ASCII
values of A…..Z are 65……90 and those of a….z are 97 ……
122. Test your program for the following data and some
random data. INPUT: Encoded Message: 2 3 1 2 1 7 9 8 6 2 3
1 0 1 9 9 5 0 1 8 7 2 3 7 9 2 3 1 0 1 8 1 1 7 9 2 7 OUTPUT:
THE DECODED MESSAGE: Have a Nice Day
Algorithm:
1.
Read Input:
2.
1. Prompt the user for the encoded message and
read it as a string.
3.
4.
74
5.
6.
Program Code:
import java.util.Scanner;
/**
* Decodes the given encoded message.
*
* @param encodedMessage The encoded message as a
string of digits separated by spaces.
* @return The decoded message as a string of characters.
*/
public static String decodeMessage(String
encodedMessage) {
// Remove spaces from the encoded message
String digits = encodedMessage.replace(" ", "");
75
for (int i = 0; i < digits.length(); i++) {
// Populate the reversedArray with characters from
the end of the digits string
reversedArray[i] = digits.charAt(digits.length() - 1 -
i);
}
// Convert the reversed character array back to a string
String reversedDigits = new String(reversedArray);
/**
* Checks if a given string represents a valid ASCII value
for a character.
*
* @param str The string representing the ASCII value.
* @return True if the ASCII value is valid (A-Z or a-z),
false otherwise.
*/
76
public static boolean isValidASCII(String str) {
// Convert the string to an integer
int value = Integer.parseInt(str);
// Check if the value is within the range of uppercase or
lowercase letters
return (value >= 97 && value <= 122) || (value >= 65
&& value <= 90);
}
}
Output:
77
78
Bibliography:
The following sites have been
referred to for the following
projects:
Blackbox coding.com
Isc help.com
Shutterstock.com
Pintrest.com
Passionate codin.com
79