Final Project
Final Project
Science
Roll. No. – 54
Index No. –
UID –
Session – 2025-26
1
Muskan Agarwal XII Science Computer
Science
Acknowledgement
I would like to express my sincere gratitude to Miss Moumita Kabi, my Computer Science teacher, for her
invaluable guidance, encouragement, and constructive feedback during the development of this Java
programming project. Her support has strengthened my understanding of programming concepts and given
me the confidence to apply them effectively. Her mentorship has been a constant source of motivation and
inspiration.
I am also deeply thankful to Sister Anjali Francis, our esteemed Principal, for fostering a positive academic
environment and ensuring the availability of resources that made the successful completion of this project
possible. Her support has greatly contributed to my learning experience.
Lastly, I extend my heartfelt appreciation to my school, my classmates, and my family. My school has
provided me with the platform to learn and grow, my classmates have encouraged me with their cooperation,
and my family has stood by me with their constant support and motivation throughout this journey.
2
Muskan Agarwal XII Science Computer
Science
______________________________ ______________________________
3
Muskan Agarwal XII Science Computer
Science
Procedural Programming
1. Definition
Procedural programming is a paradigm where code is organized into procedures (functions/methods).
Each procedure contains a set of instructions executed in order to perform a task. While Java is mainly
object-oriented, it also allows procedural programming through static methods and the main() method.
2. Features
Step-by-step execution using a top-down flow.
Procedures (methods) group code into reusable units.
Global and local variables manage data scope.
Parameter passing allows data exchange between methods.
Modularity breaks programs into smaller parts.
3. Pros and Cons
Advantages
Simple and beginner-friendly.
Code reuse through functions.
Easier debugging by testing individual methods.
Provides a clear, structured approach.
Disadvantages
Hard to scale for large projects.
Global variables reduce data security.
Maintenance becomes difficult if data structures change.
4. Examples of Procedural Languages
C, Pascal, FORTRAN, BASIC, and Java (when not using OOP features).
5. Example in Java
public class SumExample {
public static int addNumbers(int a, int b) {
return a + b; // procedure
}
7. Conclusion
4
Muskan Agarwal XII Science Computer
Science
Procedural programming in Java is a simple, function-oriented approach suited for small applications. For
larger systems, Java’s OOP features offer better modularity, security, and maintainability.
Program 01
Program:
A Composite Magic Number is a positive integer that is both composite and a magic number.
Composite number: An integer with more than two factors.
Magic number: A number whose digits, when repeatedly summed, eventually reduce to 1 (also
known as having a digital root of 1).
Examples:
Factors of 12 → 1, 2, 3, 4, 6, 12 (so 12 is composite).
46 → 4 + 6 = 10 → 1 + 0 = 1 (so 46 is a magic number).
The program accepts two positive integers m and n such that m < n. It finds and displays all composite
magic numbers within the range [m, n] (inclusive) and also prints their count.
Sample Input 1:
m = 20
n = 60
Sample Output 1:
The composite magic numbers are: 28, 46, 55
Frequency of composite magic numbers: 3
Sample Input 2:
m = 100
n = 10
Sample Output 2:
Invalid input range
Algorithm:
Solution:
5
Muskan Agarwal XII Science Computer
Science
//The following program checks whether the number entered is a magic composite number or not and
displays the frequency of composite magic numbers
import java.util.Scanner;
System.out.print("Composite magic numbers between " + m + " and " + n + " are:");
int count = 0;
if (isComposite) {
int num = i;
while (num > 9) {
int sum = 0;
while (num > 0) {
sum += num % 10;
num /= 10;
}
num = sum;
}
if (num == 1) {
count++;
System.out.print(i + " ");
}
}
}
6
Muskan Agarwal XII Science Computer
Science
System.out.println();
System.out.println("Frequency of composite magic numbers
Program Output
7
Muskan Agarwal XII Science Computer
Science
Program 02
Program
Prime Number: A number is said to be prime if it has only two factors 1 and itself.
Circular Prime Number: A prime number that remains prime under cyclic shifts of its digits. When the
leftmost digit is removed and replaced at the end of the remaining string of digits, the generated number is
still prime. The process is repeated until the original number is reached again.
Example:
131
311
113
Hence, 131 is a circular prime.
Accept a positive number N and check whether it is a circular prime or not. The new numbers formed after
the shifting of the digits should also be displayed. Test your program with the following data and some
random data:
Example 1
INPUT:
N = 197
OUTPUT:
197
971
719
197 IS A CIRCULAR PRIME.
Example 2
INPUT:
N = 1193
OUTPUT:
1193
1931
9311
3119
1193 IS A CIRCULAR PRIME.
Algorithm
Step 1: Start the program and declare class CircularPrime with instance variable number.
Step 2: Define a constructor CircularPrime(int number) to initialize this.number = number.
Step 3: Define static method isPrime(int num).
3.1 If num < 2, return false.
8
Muskan Agarwal XII Science Computer
Science
Solution
import java.util.*;
9
Muskan Agarwal XII Science Computer
Science
10
Muskan Agarwal XII Science Computer
Science
Program Output
Program 03
Program:
Kaprekar Number
A Kaprekar number is a number whose square can be split into two parts that add up to the original
number.
Example:
Number = 45
Square = 2025
Split into 20 and 25 → 20 + 25 = 45
Hence, 45 is a Kaprekar number.
11
Muskan Agarwal XII Science Computer
Science
Write a main function to create suitable objects of the class and check for Kaprekar numbers by calling the
appropriate methods.
Sample Input 1:
45
Sample Output 1:
Square of the number: 2025
20 + 25 = 45
Yes, it is a Kaprekar Number
Sample Input 2:
12
Sample Output 2:
Square of the number: 144
14 + 4 = 18
No, it is not a Kaprekar Number
Algorithm:
12
Muskan Agarwal XII Science Computer
Science
Solution:
/*This program checks whether the number entered is karprekar number or not*/
import java.util.Scanner;
public class KaprekarNumberChecker
{
//instance variables for the class
int n, sqnum;
int ctr = 0; // Initialize ctr to zero
//to input number and find its square
void numfunctions()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter any number to check for Kaprekar");
n = sc.nextInt();//inputs value for n from user
sqnum = n * n;//calculating square of number entered
}
//to count the digits of the number entered
void countdigits() {
int temp = n;
while (temp != 0) {
temp /= 10;
ctr++;
}
}
//to check whether the number entered is Karprekar number or not
void checkkaprekar() {
int powTen = (int) Math.pow(10, ctr);
int r = sqnum / powTen;//calculating the remainder
int q = sqnum % powTen;//calculating the quotient
if (r + q == n) //checking condition
{
System.out.println(n + " is a Kaprekar number");
}
else
{
System.out.println(n + " is not a Kaprekar number");
}
}
13
Muskan Agarwal XII Science Computer
Science
Program Output
Program 04
Program
An abundant number is a number whose sum of proper divisors (excluding the number itself) is greater
than the number.
Example:
Input: 12
14
Muskan Agarwal XII Science Computer
Science
Proper divisors: 1, 2, 3, 4, 6
Sum = 1 + 2 + 3 + 4 + 6 = 16
Since 16 > 12, 12 is an abundant number.
Algorithm
Step 1: Start
Step 2: Input a number n from the user
Step 3: Initialize sum = 0
Step 4: Set loop counter i = 1
Step 5: Repeat while i < n
Step 5.1: If n % i == 0, then add i to sum
sum ← sum + i
Step 5.2: Increment i by 1
i←i+1
Step 6: After loop ends, compare sum with n
Step 6.1: If sum > n, print "n is an abundant number"
Step 6.2: Otherwise, print "n is not an abundant number"
Step 7: Stop
Solution
import java.util.Scanner;
if (sum > n)
System.out.println(n + " is an abundant number.");
else
System.out.println(n + " is not an abundant number.");
}
}
Program Output
15
Muskan Agarwal XII Science Computer
Science
Program 05
Program
Smith Number:
A Smith number is a composite number (not prime) whose sum of digits is equal to the sum of the digits of
its prime factors (counting multiplicity).
Example:
Number = 666
Sum of digits = 6 + 6 + 6 = 18
Prime factors = 2 × 3 × 3 × 37
Sum of digits of prime factors = 2 + 3 + 3 + (3 + 7) = 18
Since both sums match, 666 is a Smith Number.
Algorithm
Step 1: Start
Step 2: Input a number n
Step 3: If n <= 0, print "n is not a Smith Number" and stop
Step 4: Check if n is composite
Step 4.1: Divide n by every number from 2 to n-1
Step 4.2: If n is divisible by any number, set isComposite = true
Step 5: If n is not composite (prime or equal to 1), print "n is not a Smith Number" and stop
Step 6: Find the sum of digits of n
Step 6.1: Initialize sumDigits = 0
Step 6.2: Extract each digit of n using % and add to sumDigits
Step 7: Find the sum of digits of prime factors of n
Step 7.1: Initialize sumPrimeDigits = 0
16
Muskan Agarwal XII Science Computer
Science
Solution
import java.util.Scanner;
if (n <= 0) {
System.out.println(n + " is not a Smith Number.");
return;
}
if (isComposite && n != 1) {
int sumDigits = 0;
int t = n;
while (t != 0) {
sumDigits += t % 10;
t /= 10;
}
int sumPrimeDigits = 0;
t = n;
for(int i = 2; i < t; i++) {
while(t % i == 0) {
t /= i;
17
Muskan Agarwal XII Science Computer
Science
int temp = i;
while (temp != 0) {
sumPrimeDigits += temp % 10;
temp /= 10;
}
}
}
if(t > 2) {
while (t != 0) {
sumPrimeDigits += t % 10;
t /= 10;
}
}
if (sumPrimeDigits == sumDigits)
System.out.println(n + " is a Smith Number.");
else
System.out.println(n + " is not a Smith Number.");
} else {
System.out.println(n + " is not a Smith Number.");
}
}
}
Program Output
18
Muskan Agarwal XII Science Computer
Science
Program 06
Program
A Hamming number is a positive integer whose prime factors consist of only 2, 3, and 5.
Write a program in Java to input any positive integer and check whether it is a Hamming number.
Use a main() method to create an object of the class and call the member methods accept() and check().
Algorithm
Step 1: Start
Step 2: Initialize instance variable: num ← 0
Step 3: In main(), create an object of the class and call methods accept() and check()
Step 4: Define method accept() to input the number into variable num
Step 5: Define method check()
Step 5.1: If num < 0, print "NEGATIVE NUMBER ENTERED. INVALID INPUT" and return
Step 5.2: If num == 0, print "0 IS NOT A HAMMING NUMBER" and return
Step 5.3: Set original ← num
Step 5.4: Set isHamming ← true
Step 5.5: Print original followed by " = "
Step 5.6: Set primes ← {2, 3, 5} and firstFactor ← true
Step 5.7: For each prime p in primes, repeat while num % p == 0:
Step 5.7.1: If firstFactor == false, print " × "
Step 5.7.2: Print p
Step 5.7.3: Set num ← num / p
Step 5.7.4: Set firstFactor ← false
Step 5.8: If num > 1, print " × " + num and set isHamming ← false
Step 5.9: If isHamming == true, print original + " IS A HAMMING NUMBER."
Else, print original + " IS NOT A HAMMING NUMBER."
Step 6: Stop
Solution
import java.util.Scanner;
class Hamming {
int num;
19
Muskan Agarwal XII Science Computer
Science
if (num > 1) {
System.out.print(" × " + num);
isHamming = false;
}
System.out.println();
if (isHamming)
System.out.println(original + " IS A HAMMING NUMBER.");
else
System.out.println(original + " IS NOT A HAMMING NUMBER.");
}
20
Muskan Agarwal XII Science Computer
Science
obj.check();
}
}
Program Output
Program 07
Program
A Trinomial Triangle is a triangular arrangement of trinomial coefficients.
It is constructed starting with a single 1 in the first row, the second row containing three 1s, and each
subsequent element obtained by summing the values from the previous row: one above-left, one directly
above, and one above-right.
Example (first few rows):
1
111
12321
1367631
1 4 10 16 19 16 10 4 1
21
Muskan Agarwal XII Science Computer
Science
Write a program to accept the number of rows from the user and generate the Trinomial Triangle using
recursion (or iterative array-based logic).
Algorithm
Step 1: Start
Step 2: Input the number of rows → rows
Step 3: Create a 2D array triangle[rows][2*rows+1] to store trinomial coefficients
Step 4: Find the middle index: mid ← rows (to center the triangle)
Step 5: Assign base case: triangle[0][mid] ← 1
Step 6: For each row i from 1 to rows-1:
Step 6.1: For each column j from 1 to 2*rows-1:
triangle[i][j] ← triangle[i-1][j-1] + triangle[i-1][j] + triangle[i-1][j+1]
Step 7: Print the triangle:
Step 7.1: For each element, if non-zero → print number
Step 7.2: Otherwise → print spaces for formatting
Step 8: Stop
Solution
import java.util.Scanner;
// Base case
triangle[0][mid] = 1;
22
Muskan Agarwal XII Science Computer
Science
Program 08
Program
23
Muskan Agarwal XII Science Computer
Science
A Dudeney Number is a positive integer that is a perfect cube, and the sum of its digits is equal to its cube
root.
Example:
Input: 512
Sum of digits = 5 + 1 + 2 = 8
Cube root of 512 = 8
Since both are equal → 512 is a Dudeney Number.
Write a program in Java to check whether a given number is a Dudeney Number or not.
Algorithm
Step 1: Start
Step 2: Input a number → number
Step 3: Check if number is a perfect cube:
Compute cubeRoot = cbrt(number)
If cubeRoot³ ≠ number, then print → not a Dudeney number, and stop.
Step 4: Compute the sum of digits of number:
Initialize s = 0
Repeat while number > 0
Extract digit: digit = number % 10
Add digit to s
Update: number = number / 10
Step 5: If s == cubeRoot, print → it is a Dudeney number
Step 6: Else, print → it is not a Dudeney number
Step 7: Stop
Solution
import java.util.Scanner;
24
Muskan Agarwal XII Science Computer
Science
sum += digit;
temp /= 10;
}
if (sum == cubeRoot) {
System.out.println(number + " is a Dudeney number");
} else {
System.out.println(number + " is not a Dudeney number");
}
} else {
System.out.println(number + " is not a Dudeney number");
}
}
}
Program Output
Program 09
Program
Write a program in Java to check whether a number is a Golden Batch Number. A Golden Batch Number is a
number that is divisible by the sum of its digits.
Algorithm
25
Muskan Agarwal XII Science Computer
Science
Step 1: Start
Step 2: Input a number from the user
Step 3: Initialize sum = 0
Step 4: Set temp = number
Step 5: Repeat while temp != 0:
5.1 sum += temp % 10
5.2 temp /= 10
Step 6: Display the sum of digits
Step 7: If number % sum == 0, then:
7.1 Print “It is a Golden Batch Number”
Else:
7.2 Print “It is not a Golden Batch Number”
Step 8: Stop
Solution
import java.util.Scanner;
int sum = 0;
int temp = n;
while (temp != 0) {
sum += temp % 10;
temp /= 10;
}
if (n % sum == 0) {
System.out.println(n + " is a Golden Batch Number");
} else {
System.out.println(n + " is not a Golden Batch Number");
}
sc.close();
}
}
26
Muskan Agarwal XII Science Computer
Science
Program Output
Program 10
Program
Write a Java program to perform Binary Search on a list of numbers using recursion. The program should
accept a list of n elements from the user, sort them in ascending order using Bubble Sort, and then accept a
value to be searched. The binary search technique should be implemented recursively to determine whether
the value exists in the list. If the value is found, the program should display its position; otherwise, it should
indicate that the value is not present in the array.
Algorithm
Step 1: Start.
Step 2: Input the number of elements n.
Step 3: Read n elements into an array arr.
Step 4: Sort the array in ascending order using Bubble Sort.
Step 5: Input the value v to be searched.
Step 6: Perform binary search recursively:
6.1 Compute m = (l + u) / 2 where l = lower index, u = upper index.
6.2 If arr[m] == v, return m.
6.3 If l > u, return -1 (element not found).
27
Muskan Agarwal XII Science Computer
Science
Solution
import java.util.*;
class BinSearch {
int arr[];
int n;
static Scanner sc = new Scanner(System.in);
BinSearch(int nn) {
n = nn;
}
void fillArray() {
arr = new int[n];
System.out.println("Enter " + n + " elements:");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
}
void sort() {
int t;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
t = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = t;
}
}
}
}
28
Muskan Agarwal XII Science Computer
Science
if (location != -1) {
System.out.println(v + " found at position: " + (location + 1));
} else {
System.out.println(v + " not found in the array.");
}
}
}
Program Output
29
Muskan Agarwal XII Science Computer
Science
Program 11
30
Muskan Agarwal XII Science Computer
Science
Program
Write a program in Java to demonstrate the Unique Prime Factorization Theorem (Fundamental
Theorem of Arithmetic). The program should accept an integer n (greater than 1) from the user, find its
prime factors, and display the product of those prime factors. This shows that the factorization of any integer
greater than 1 into prime numbers is unique (the order of factors does not matter).
Algorithm
Step 1: Start.
Step 2: Input an integer n (where n > 1).
Step 3: Store the original value of n in a variable temp.
Step 4: Display the message: "Unique Prime Factorization of n = ".
Step 5: Set i = 2.
Step 6: Repeat steps 7–10 while i ≤ temp.
Step 7: If n is divisible by i, then:
7.1 Print i (as a prime factor).
7.2 Divide n by i (n = n / i).
7.3 If n is still greater than 1, print the multiplication symbol " × ".
7.4 Repeat Step 7 for the same i.
Step 8: Otherwise, increase i by 1 (i = i + 1).
Step 9: Continue until n becomes 1.
Step 10: Stop the loop.
Step 11: End.
Solution
import java.util.Scanner;
31
Muskan Agarwal XII Science Computer
Science
if (n > 1) {
System.out.print(" × "); // Display multiplication sign
}
}
}
}
}
Program Output
Program 12
Program
Write a Java program to find the Saddle Point in a square matrix.
A Saddle Point is an element of a matrix that is:
1. The minimum element in its row, and
2. The maximum element in its column.
If such an element exists, print it as the Saddle Point; otherwise, display “No Saddle Point”.
Algorithm
Step 1: Start.
Step 2: Input the size of the matrix n.
Step 3: If n > 20, print “Invalid Input” and stop.
Step 4: Create a 2D array a[n][n] and input all elements.
Step 5: Print the original matrix.
Step 6: For each row i of the matrix:
6.1 Find the minimum element rMin in that row.
6.2 Store its column index cIdx.
Step 7: Check the entire column cIdx:
If rMin is greater than or equal to every element in column cIdx, then rMin is a Saddle Point.
Print the Saddle Point and stop.
Step 8: If no such element is found in any row, print “No Saddle Point”.
Step 9: Stop.
32
Muskan Agarwal XII Science Computer
Science
Solution
import java.util.*;
a = new int[n][n];
System.out.println("Enter the elements of the matrix:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = sc.nextInt();
}
}
33
Muskan Agarwal XII Science Computer
Science
if (isSaddle) {
System.out.println("Saddle Point: " + rMin);
found = true;
break;
}
}
if (!found) {
System.out.println("No Saddle Point");
}
}
34
Muskan Agarwal XII Science Computer
Science
Program 13
Program
35
Muskan Agarwal XII Science Computer
Science
Algorithm
Step 1: Start.
Step 2: Input the order of the matrix N (where 2 < N < 10).
Step 3: Declare a 2D array arr[N][N].
Step 4: Input all the elements of the matrix.
Step 5: Display the original matrix.
Step 6: For each row i from 0 to N-1:
For each column j from 0 to N-1:
Print the element at arr[i][N-1-j].
Step 7: This forms the mirror image matrix.
Step 8: Stop.
Solution
import java.util.Scanner;
36
Muskan Agarwal XII Science Computer
Science
System.out.print(arr[i][j] + "\t");
}
System.out.println();
}
37
Muskan Agarwal XII Science Computer
Science
38
Muskan Agarwal XII Science Computer
Science
Program 14
Program
Write a program to declare a character matrix M[N][N], where N must be greater than 3 and less than 10.
Accept three different characters from the user and fill the array as follows:
1. Fill the four corners of the square matrix with the first character.
2. Fill the boundary elements except corners with the second character.
3. Fill the non-boundary elements with the third character.
If the entered size is not in the range 4–9, display "SIZE OUT OF RANGE".
Example 1:
INPUT: N = 4
FIRST CHARACTER: @
SECOND CHARACTER: ?
THIRD CHARACTER: #
OUTPUT:
@??@
?##?
?##?
@??@
Example 2:
INPUT: N = 5
FIRST CHARACTER: A
SECOND CHARACTER: C
THIRD CHARACTER: X
OUTPUT:
ACCCA
CXXXC
CXXXC
CXXXC
ACCCA
Example 3:
INPUT: N = 15
OUTPUT: SIZE OUT OF RANGE
Algorithm
Step 1: Start.
Step 2: Input size m of the matrix.
Step 3: If m < 4 OR m > 9, print "SIZE OUT OF RANGE" and stop.
39
Muskan Agarwal XII Science Computer
Science
Solution
import java.util.*;
class Matrix {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
40
Muskan Agarwal XII Science Computer
Science
sc.close();
}
}
Program Output
41
Muskan Agarwal XII Science Computer
Science
42
Muskan Agarwal XII Science Computer
Science
Program 15
Program
Write a program in Java to check whether a given square matrix of order N × N is a Wondrous Square.
A Wondrous Square is defined as:
1. It contains all numbers from 1 to N × N exactly once.
2. The sum of each row and each column is equal to the same constant (called the Wondrous
Number).
Algorithm
Step 1: Start
Step 2: Fix N = 4.
Step 3: Declare a 2D array arr[N][N].
Step 4: Input all elements of the matrix.
Step 5: Check if the matrix contains all numbers from 1 to N × N exactly once.
If not, print “Not a Wondrous Square” and stop.
Step 6: Calculate the sum of the first row. Store it as wondrousSum.
Step 7: For each row i:
Find the sum of row i.
If the sum ≠ wondrousSum, print “Not a Wondrous Square” and stop.
Step 8: For each column j:
Find the sum of column j.
If the sum ≠ wondrousSum, print “Not a Wondrous Square” and stop.
Step 9: If all checks pass, print “It is a Wondrous Square with wondrous sum = …”.
Step 10: End
Solution
import java.util.Scanner;
// Input
System.out.println("Enter elements of the matrix:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
43
Muskan Agarwal XII Science Computer
Science
a[i][j] = in.nextInt();
}
}
// Display matrix
System.out.println("\nThe Matrix is:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(a[i][j] + "\t");
}
System.out.println();
}
// Check if Wondrous
if (isWondrous(a)) {
int wondrousSum = 0;
for (int j = 0; j < n; j++) {
wondrousSum += a[0][j]; // sum of first row
}
System.out.println("\nIt is a Wondrous Square with wondrous sum = " + wondrousSum);
} else {
System.out.println("\nNot a Wondrous Square");
}
in.close();
}
44
Muskan Agarwal XII Science Computer
Science
int wondrousSum = 0;
for (int j = 0; j < n; j++) {
wondrousSum += arr[0][j];
}
// 3. Check rows
for (int i = 0; i < n; i++) {
int rowSum = 0;
for (int j = 0; j < n; j++) {
rowSum += arr[i][j];
}
if (rowSum != wondrousSum) return false;
}
// 4. Check columns
for (int j = 0; j < n; j++) {
int colSum = 0;
for (int i = 0; i < n; i++) {
colSum += arr[i][j];
}
if (colSum != wondrousSum) return false;
}
return true;
}
}
Program Output
45
Muskan Agarwal XII Science Computer
Science
46
Muskan Agarwal XII Science Computer
Science
seen boolean[] Used to track whether each number from 1 to 16 is present exactly once
in Scanner Used to take input from user
Program 16
Program
Algorithm
Step 1: Start
Step 2: Declare variables: str (input text), cipher (encrypted text), len (length of text), ch (character for
processing).
Step 3: Accept user input string and store in str. Compute its length as len.
Step 4: If len <= 3 or len >= 100, print "INVALID LENGTH" and stop.
Step 5: Otherwise, initialize an empty string cipher.
Step 6: For each character ch in str:
6.1 If ch is between 'A'–'M' or 'a'–'m', shift it forward by 13.
47
Muskan Agarwal XII Science Computer
Science
Solution
import java.util.Scanner;
if ((ch >= 'A' && ch <= 'M') || (ch >= 'a' && ch <= 'm')) {
cipher.append((char)(ch + 13));
}
else if ((ch >= 'N' && ch <= 'Z') || (ch >= 'n' && ch <= 'z')) {
cipher.append((char)(ch - 13));
}
else {
cipher.append(ch); // Non-alphabetic characters unchanged
}
}
sc.close();
}
48
Muskan Agarwal XII Science Computer
Science
}
Program Output
Program 17
Program
Write a program in Java to accept a sentence and a word from the user and find the frequency of occurrence
of the given word in the sentence.
Algorithm
Step 1: Start
Step 2: Input a sentence from the user.
Step 3: Input the word to be searched in the sentence.
Step 4: Convert the sentence and the word into lowercase form (to ignore case differences).
Step 5: Split the sentence into individual words using space as a delimiter.
Step 6: Initialize a counter variable count to 0.
Step 7: Repeat for each word in the array of words:
49
Muskan Agarwal XII Science Computer
Science
Solution
import java.util.Scanner;
sentence = sentence.toLowerCase();
word = word.toLowerCase();
int count = 0;
for (String w : words) {
if (w.equals(word)) {
count++;
}
}
}
}
Program Output
50
Muskan Agarwal XII Science Computer
Science
Program 18
Program
Write a program in Java to input a sentence. Calculate the potential of each word and display it. The
potential of a word is the sum of the positions of its alphabets in the English alphabetic order (i.e., a = 1, b =
2, …, z = 26). Finally, display the potentials of all words in ascending order.
Algorithm
Step 1: Start
Step 2: Input a sentence from the user.
Step 3: Tokenize the sentence into individual words using StringTokenizer with delimiters like space,
comma, period, semicolon, etc.
Step 4: Initialize an integer array potentials[100] to store the potential values of words.
Step 5: Initialize count = 0.
Step 6: Repeat while there are more tokens (words):
51
Muskan Agarwal XII Science Computer
Science
Solution
import java.util.Scanner;
import java.util.StringTokenizer;
// Input sentence
System.out.println("Enter a sentence:");
String sentence = sc.nextLine();
52
Muskan Agarwal XII Science Computer
Science
int count = 0;
System.out.println("\nWord potentials:");
while (st.hasMoreTokens()) {
String word = st.nextToken();
int pot = getPotential(word);
potentials[count] = pot;
System.out.println(word + ": " + pot);
count++;
}
sc.close();
}
}
Program Output
53
Muskan Agarwal XII Science Computer
Science
Program 19
Program
Write a program in Java to input a sentence, capitalize the first letter of each word, and display the sentence
in reverse order of words.
Algorithm
54
Muskan Agarwal XII Science Computer
Science
Step 1: Start
Step 2: Input a sentence from the user
Step 3: Tokenize the sentence into individual words using StringTokenizer with space and punctuation as
delimiters
Step 4: Initialize an array words[100] to store capitalized words and count = 0
Step 5: While there are more tokens:
5.1 Extract the next word
5.2 Capitalize the first letter of the word and convert the rest to lowercase
5.3 Store the capitalized word in words[count]
5.4 Increment count by 1
Step 6: Print the words in reverse order from words[count-1] to words[0]
Step 7: Stop
Solution
import java.util.Scanner;
import java.util.StringTokenizer;
System.out.println("Enter a sentence:");
String sentence = sc.nextLine();
while (st.hasMoreTokens()) {
String word = st.nextToken();
word = word.substring(0, 1).toUpperCase() + word.substring(1).toLowerCase();
words[count] = word;
count++;
}
System.out.println("\nReversed sentence:");
for (int i = count - 1; i >= 0; i--) {
System.out.print(words[i] + " ");
}
sc.close();
}
55
Muskan Agarwal XII Science Computer
Science
Program Output
Program 20
Program
Write a program in Java to input a sentence, calculate the frequency of each word, and display the words in
ascending order of their frequency.
Algorithm
Step 1: Start
Step 2: Input a sentence from the user
Step 3: Tokenize the sentence into words using StringTokenizer with delimiters like space, comma, period,
semicolon, etc.
56
Muskan Agarwal XII Science Computer
Science
Solution
import java.util.Scanner;
import java.util.StringTokenizer;
System.out.println("Enter a sentence:");
String sentence = sc.nextLine();
while (st.hasMoreTokens()) {
words[count] = st.nextToken().toLowerCase();
count++;
}
57
Muskan Agarwal XII Science Computer
Science
sc.close();
}
}
Program Output
58
Muskan Agarwal XII Science Computer
Science
59