Programs Print
Programs Print
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:
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:
OUTPUT:
1)
Enter number
15
It is a Deficient Number
2)
Enter number
18
It is an Abundant Number
Program 4:
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;
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
}
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.*;
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.*;
// Input sentences
System.out.print("Enter the first sentence: ");
String sentence1 = scanner.nextLine();
System.out.print("Enter the second sentence: ");
String sentence2 = scanner.nextLine();
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
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();
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:
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:
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