Program 1:
import java.util.*;
class proj1 {
// A recursive function to find nth catalan number
int catalan(int n)
{
int res = 0;
// Base case
if (n <= 1) {
return 1;
}
for (int i = 0; i < n; i++) {
res += catalan(i) * catalan(n - i - 1);
}
return res;
}
// Driver Code
public static void main(String[] args)
{
Scanner as=new Scanner(System.in);
System.out.println("Enter a number");
int p=as.nextInt();
proj1 cn = new proj1();
System.out.print("Catalan number is "+ cn.catalan(p));
}
}
OUTPUT:
1)
Enter a number
12
Catalan number is 208012
2)
Enter a number
5
Catalan number is 42
Program 2:
import java.util.Scanner; // Importing the Scanner class for
user input
public class proj2 {
public static void main(String[] args) {
Scanner as = new Scanner(System.in); // Creating a
Scanner object for input
System.out.println("Enter number whose multi factorial
is to be calculated");
// Prompting the user to enter a number
int x = as.nextInt(); // Reading the user input and storing
it in variable x
System.out.println("Enter factor (factor should be
greater than 1): ");
// Prompting the user to enter a factor
int fact = as.nextInt(); // Reading the user input and
storing it in variable fact
int f = 1; // Initializing the result variable f to 1
int l = fact; // Storing the factor in variable l (not
necessary, but included for clarity)
// Loop to calculate the multi-factorial
for (int i = x; i > 0; i -= l) {
f *= i; // Multiplying the result by the current value of i
}
// Printing the result
System.out.println("Multi-Factorial (factor=" + fact + ")
for " + x + " is " + f);
}
}
OUTPUT:
1)
Enter number whose multi factorial is to be calculated
3
Enter factor (factor should be greater than 1):
4
Multi-Factorial (factor=4) for 3 is 3
2)
Enter number whose multi factorial is to be calculated
5
Enter factor (factor should be greater than 1):
3
Multi-Factorial (factor=3) for 5 is 10
Program 3:
import java.util.Scanner; // Importing the Scanner class for
user input
public class proj3 {
public static void main(String[] args) {
Scanner as = new Scanner(System.in); // Creating a
Scanner object for input
System.out.println("Enter number"); // Prompting the
user to enter a number
int x = as.nextInt(); // Reading the user input and storing
it in variable x
int sum = 0; // Initializing the sum variable to 0
// Loop to calculate the sum of all divisors of x
for (int i = 1; i <= x; i++) {
if (x % i == 0) // Checking if i is a divisor of x
sum += i; // Adding the divisor to the sum
}
// Checking the type of number based on the sum of its
divisors
if (2 * x > sum)
System.out.println("It is a Deficient Number");
// If 2*x is greater than the sum, it is a Deficient
Number
else if (2 * x == sum)
System.out.println("It is a Perfect Number");
// If 2*x equals the sum, it is a Perfect Number
else
System.out.println("It is an Abundant Number");
// If 2*x is less than the sum, it is an Abundant Number
}
}
OUTPUT:
1)
Enter number
15
It is a Deficient Number
2)
Enter number
18
It is an Abundant Number
Program 4:
import java.util.*; // Importing the Scanner class for user
input
public class proj4 {
public static void main(String[] args) {
Scanner as = new Scanner(System.in); // Creating a
Scanner object for input
System.out.println("Enter a number"); // Prompting the
user to enter a number
double x = as.nextDouble(); // Reading the user input
and storing it in variable x
double sum = 0.0; // Initializing the sum variable to 0.0
// Loop to calculate the sum of the series
for (double i = 1; i <= x; i++) {
int fact = 1; // Initializing the factorial variable to 1 for
each i
// Inner loop to calculate the factorial of i
for (int j = 1; j <= i; j++) {
fact *= j; // Calculating the factorial of i
}
double l = Math.pow(x, i); // Calculating x raised to the
power of i
// Adding or subtracting the term based on the value
of i
if (i % 2 == 0)
sum -= (l / fact); // If i is even, subtract the term
from sum
else
sum += (l / fact); // If i is odd, add the term to sum
}
// Printing the result
System.out.println("Sum of Series is : " + sum);
}
}
OUTPUT:
Enter a number
6
Sum of Series is : -30.0
Program 5:
import java.util.*; // Importing the Scanner class for user
input
public class proj5 {
public static void main(String[] args) {
Scanner as = new Scanner(System.in); // Creating a
Scanner object for input
System.out.println("Enter two numbers"); //
Prompting the user to enter two numbers
int a = as.nextInt(); // Reading the first number and
storing it in variable a
int b = as.nextInt(); // Reading the second number
and storing it in variable b
int gcd; // Variable to store the Greatest Common
Divisor (GCD)
int l = b; // Storing the value of b in l for LCM
calculation later
int p = a; // Storing the value of a in p for LCM
calculation later
// Loop to calculate the GCD using the Euclidean
algorithm
while (b != 0) {
gcd = b; // Storing the value of b in gcd
b = a % b; // Calculating the remainder of a
divided by b
a = gcd; // Setting a to the current gcd value
}
// Calculating the Least Common Multiple (LCM) using
the formula: LCM(a, b) = (a * b) / GCD(a, b)
double lcm = (double)(p * l) / a;
// Printing the GCD and LCM
System.out.println("The Greatest Common Divisor of the
2 numbers is : " + a + "\nThe Lowest Common Multiple of the
2 numbers is : " + lcm);
}
}
OUTPUT:
Enter two numbers
16
40
The Greatest Common Divisor of the 2 numbers is : 8
The Lowest Common Multiple of the 2 numbers is : 80.0
Program 6:
import java.util.Scanner;
public class proj6 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a 6-digit number (ddMMyy): ");
// Read input as an integer
Int inputNumber = scanner.nextInt();
// Convert the integer to a string with leading zeros if necessary
String input = Integer.toString(inputNumber);
// Validate the input length
if (input.length() != 6) {
System.out.println("Invalid input! Please enter exactly 6
digits.");
return;
}
// Parse the input
int day = Integer.parseInt(input.substring(0, 2));
int month = Integer.parseInt(input.substring(2, 4));
int year = Integer.parseInt(input.substring(4, 6)) + 2000; //
Assuming 20xx for simplicit
// Validate the month
if (month < 1 || month > 12) {
System.out.println("Invalid month! Please ensure the month is
between 01 and 12.");
return;
}
// Validate the day based on the month
if (day < 1 || day > 31 || (month == 2 && day > 29) ||
(month == 2 && day == 29 && !isLeapYear(year)) ||
((month == 4 || month == 6 || month == 9 || month == 11)
&& day > 30)) {
System.out.println("Invalid day! Please ensure the day is valid
for the given month.");
return;
}
// Convert month number to month name
String monthName = "";
switch (month) {
case 1: monthName = "January"; break;
case 2: monthName = "February"; break;
case 3: monthName = "March"; break;
case 4: monthName = "April"; break;
case 5: monthName = "May"; break;
case 6: monthName = "June"; break;
case 7: monthName = "July"; break;
case 8: monthName = "August"; break;
case 9: monthName = "September"; break;
case 10: monthName = "October"; break;
case 11: monthName = "November"; break;
case 12: monthName = "December"; break; }
// Format and display the date
System.out.println("Converted Date:"+day+"
"+monthName+","+year); }
// Helper method to determine if a year is a leap year
private static boolean isLeapYear(int year) {
if (year % 4 == 0) {
if (year % 100 == 0) {
return year % 400 == 0; }
return true; }
return false;
} }
OUTPUT:
1)
Enter a 6-digit number (ddMMyy):
110724
Converted Date:11 July,2024
2)
Enter a 6-digit number (ddMMyy):
11
Invalid input! Please enter exactly 6 digits.
Program 7:
import java.util.Scanner;
public class proj7 {
// Method to reverse an integer
public static int reverse(int num) {
int rev = 0
while (num != 0) {
int d = num % 10; // Extracting the last digit
rev = rev * 10 + d; // Appending the last digit to the
reversed number
num /= 10; // Removing the last digit
}
return rev;
}
// Method to check if a number is an Adam number
public static boolean isAdam(int num) {
int sqNum = num * num; // Calculating the square of the
number
int revNum = reverse(num); // Reversing the number
int sqRevNum = revNum * revNum; // Calculating the
square of the reversed number
int rev = reverse(sqNum); // Reversing the square of the
number
return rev == sqRevNum; // Checking if the reversed
square is equal to the square of the reversed number
}
// Method to check if a number is prime
public static boolean isPrime(int num) {
int c = 0;
for (int i = 1; i <= num; i++) {
if (num % i == 0) {
c++; // Counting the number of factors }
}
return c == 2; // If the number has exactly 2 factors (1
and itself), it's prime }
// Main method
public static void main(String args[]) {
Scanner in = new Scanner(System.in); // Creating Scanner
object for user input
System.out.print("Enter the value of m: ");
int m = in.nextInt(); // Reading input for m
System.out.print("Enter the value of n: ");
int n = in.nextInt(); // Reading input for n
int count = 0; // Initializing counter for prime-adam
integers
if (m >= n) {
System.out.println("INVALID INPUT"); // If m is greater than
or equal to n, it's an invalid input
Return; }
System.out.println("THE PRIME-ADAM INTEGERS ARE:");
for (int i = m; i <= n; i++) {
boolean adam = isAdam(i); // Checking if the number
is Adam
if (adam) {
boolean prime = isPrime(i); // Checking if the
number is prime
if (prime) {
System.out.print(i + " "); // Printing the prime-
adam integer
count++; // Incrementing the counter }
} }
if (count == 0) {
System.out.print("NIL"); // If there are no prime-adam
integers, print NIL
}
System.out.println();
System.out.println("FREQUENCY OF PRIME-ADAM
INTEGERS IS: " + count); // Printing the frequency of prime-
adam integers
}
}
OUTPUT:
1)
Enter the value of m: 1
Enter the value of n: 100
THE PRIME-ADAM INTEGERS ARE:
2 3 11 13 31
FREQUENCY OF PRIME-ADAM INTEGERS IS: 5
2)
Enter the value of m: 100
Enter the value of n: 200
THE PRIME-ADAM INTEGERS ARE:
101 103 113
FREQUENCY OF PRIME-ADAM INTEGERS IS: 3
Program 8:
import java.util.*;
public class proj8 {
public static void main(String[] args) {
Scanner as = new Scanner(System.in);
System.out.println("Enter lower and upper limit
respectively");
int m = as.nextInt();
int n = as.nextInt();
System.out.println("Fascinating numbers between the
range are: ");
for (int i = m; i <= n; i++) {
if (fascinating(i))
System.out.print(i + " ");
}
}
public static boolean fascinating(int j) {
int num = j;
int n2 = num * 2;
int n3 = num * 3;
// concatenating num, n2, and n3
String concatstr = num + "" + n2 + n3;
boolean found = true;
// checks all digits from 1 to 9 are present or not
for (char c = '1'; c <= '9'; c++) {
int count = 0;
// loop counts the frequency of each digit
for (int i = 0; i < concatstr.length(); i++) {
char ch = concatstr.charAt(i);
// compares the character of concatstr with i
if (ch == c)
// increments the count by 1 if the specified
condition returns true
count++;
}
// returns true if any of the condition returns true
if (count > 1 || count == 0) {
found = false;
break;
}
}
return found;
}
}
OUTPUT:
1)
Enter lower and upper limit respectively
1
1000
Fascinating numbers between the range are:
192 219 273 327
2)
Enter lower and upper limit respectively
1000
2000
Fascinating numbers between the range are:
1902 1920
Program 9:
import java.util.*;
public class proj9 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input sentences
System.out.print("Enter the first sentence: ");
String sentence1 = scanner.nextLine();
System.out.print("Enter the second sentence: ");
String sentence2 = scanner.nextLine();
// Split sentences into words
String[] words1 = sentence1.split("\\s+");
String[] words2 = sentence2.split("\\s+");
// Find common words
Set<String> set1 = new
HashSet<>(Arrays.asList(words1));
Set<String> set2 = new
HashSet<>(Arrays.asList(words2));
set1.retainAll(set2);
// Display common words and their frequencies
System.out.println("\nCommon words and their
frequencies:");
System.out.println("Word\tFrequency");
System.out.println("------------------");
for (String word : set1) {
int frequency1 = countFrequency(words1, word);
int frequency2 = countFrequency(words2, word);
System.out.println(word + "\t\t" + (frequency1 +
frequency2));
}
}
// Method to count the frequency of a word in an array of
words
public static int countFrequency(String[] words, String
target) {
int count = 0;
for (String word : words) {
if (word.equals(target)) {
count++;
}
}
return count;
}
}
OUTPUT:
1
Enter the first sentence: Asia is the largest continent in the
world
Enter the second sentence: Pacific Ocean is the largest water
body in the world
Common words and their frequencies:
Word Frequency
------------------
the 4
largest 2
world 2
in 2
is 2
Program 10:
import java.util.*;
public class proj10 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input dimensions of the matrix
System.out.print("Enter the number of rows (m): ");
int m = scanner.nextInt();
System.out.print("Enter the number of columns (n): ");
int n = scanner.nextInt();
// Input matrix elements
int[][] mat = new int[m][n];
System.out.println("Enter the elements of the matrix:");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
mat[i][j] = scanner.nextInt();
} }
// Display the matrix
System.out.println("Matrix:");
displayMatrix(mat);
// Calculate sum of boundary elements
int sum = calculateSum(mat, m, n);
// Sort matrix in ascending order
int[][] sortedMat = sortMatrix(mat);
// Calculate sum of sorted boundary elements
int sortedSum = calculateSum(sortedMat, m, n);
// Output results
System.out.println("Sum of boundary elements: " +
sum);
System.out.println("Sum of boundary elements sorted in
descending order: " + sortedSum);
System.out.println("Sorted Matrix:");
displayMatrix(sortedMat);
}
// Method to display a matrix
public static void displayMatrix(int[][] mat) {
for (int[] row : mat) {
for (int cell : row) {
System.out.print(cell + "\t");
}
System.out.println();
} }
// Method to calculate sum of boundary elements
public static int calculateSum(int[][] mat, int m, int n) {
int sum = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (i == 0 || i == m - 1 || j == 0 || j == n - 1) {
sum += mat[i][j];
} } }
return sum; }
// Method to sort the matrix
public static int[][] sortMatrix(int[][] mat) {
int rows = mat.length;
int cols = mat[0].length;
int[] flat = new int[rows * cols];
int idx = 0;
// Flatten the matrix
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
flat[idx++] = mat[i][j];
} }
// Sort the flattened array
Arrays.sort(flat);
// Fill the sorted values back into the matrix
idx = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
mat[i][j] = flat[idx++];
} }
return mat;
} }
OUTPUT:
Enter the number of rows (m): 3
Enter the number of columns (n): 3
Enter the elements of the matrix:
21 19 35 61 43 2 7 52 6
Matrix:
21 19 35
61 43 2
7 52 6
Sum of boundary elements: 203
Sum of boundary elements sorted in descending order: 225
Sorted Matrix:
2 6 7
19 21 35
43 52 61
Program 11:
import java.util.Scanner;
public class proj11 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number n: ");
int n = scanner.nextInt();
// Initialize the sum
long sum = 0;
// Loop to accumulate the sum
long factorial = 1;
for (int i = 1; i <= n; i++) {
factorial *= i; // Calculate factorial of i
sum += factorial; // Accumulate sum of
factorials
}
System.out.println("The sum of the series is: "
+ sum);
}
}
OUTPUT:
1)
Enter a number n:
5
The sum of the series is: 153
2)
Enter a number n:
7
The sum of the series is: 5913
Program 12:
import java.util.Scanner;
public class proj12 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input an integer
System.out.print("Enter an integer: ");
int num = scanner.nextInt();
// Convert to binary
String binary = decimalToBinary(num);
// Output the result
System.out.println("Binary representation: " + binary);
}
// Method to convert decimal to binary
public static String decimalToBinary(int num) {
if (num == 0) {
return "0";
}
StringBuilder binary = new StringBuilder();
while (num > 0) {
int remainder = num % 2;
binary.insert(0, remainder); // Insert remainder at the
beginning
num /= 2;
}
return binary.toString();
}
}
OUTPUT:
1)
Enter an integer: 45
Binary representation: 101101
2)
Enter an integer: 100
Binary representation: 1100100
Program 13:
import java.util.Scanner;
public class proj13 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input an integer
System.out.print("Enter an integer: ");
int num = scanner.nextInt();
// Convert to octal
String octal = decimalToOctal(num);
// Output the result
System.out.println("Octal representation: " + octal);
}
// Method to convert decimal to octal
public static String decimalToOctal(int num) {
if (num == 0) {
return "0";
}
StringBuilder octal = new StringBuilder();
while (num > 0) {
int remainder = num % 8;
octal.insert(0, remainder); // Insert remainder at the
beginning
num /= 8;
}
return octal.toString();
}
}
OUTPUT:
1)
Enter an integer: 81
Octal representation: 121
2)
Enter an integer: 30
Octal representation: 36
Program 14:
import java.util.Scanner;
public class DecimalToHexadecimal {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input an integer
System.out.print("Enter an integer: ");
int num = scanner.nextInt();
// Convert to hexadecimal
String hexadecimal = decimalToHexadecimal(num);
// Output the result
System.out.println("Hexadecimal representation: " +
hexadecimal);
}
// Method to convert decimal to hexadecimal
public static String decimalToHexadecimal(int num) {
if (num == 0) {
return "0";
}
StringBuilder hexadecimal = new StringBuilder();
while (num > 0) {
int remainder = num % 16;
hexadecimal.insert(0, getHexDigit(remainder)); // Insert
remainder at the beginning
num /= 16;
}
return hexadecimal.toString();
}
// Method to get hexadecimal digit for a decimal remainder
public static char getHexDigit(int remainder) {
if (remainder < 10) {
return (char) ('0' + remainder);
} else {
return (char) ('A' + (remainder - 10)); }
} }
OUTPUT:
1)
Enter an integer: 59
Hexadecimal representation: 3B
2)
Enter an integer: 192
Hexadecimal representation: C0