1) Write a program in java to calculate and print the corresponding day number of the year in the range 1-
[Link] consists of the month number (m), the day of the month (d) and the year(y).
Sol.
import [Link];
public class DayOfYear {
public static void main(String[] args) {
Scanner in = new Scanner([Link]);
[Link]("Enter month number (1-12): ");
int m = [Link]();
[Link]("Enter day of month: ");
int d = [Link]();
[Link]("Enter year: ");
int y = [Link]();
int[] daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,
31};
// Check for leap year
if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0)) {
daysInMonth[1] = 29;
}
int dayOfYear = 0;
for (int i = 0; i < m - 1; i++) {
dayOfYear += daysInMonth[i];
}
dayOfYear += d;
[Link]("Day number of the year: " + dayOfYear);
[Link]();
}
}
Output:
Enter month number (1-12): 5
Enter day of month: 3
Enter year: 1996
Day number of the year: 124
2) write a program in java to input a matrix and check whether it is symmetric or not.
Sol.
import [Link];
public class symmetric_matrix {
public static void main(String[] args) {
Scanner in = new Scanner([Link]);
[Link]("Enter order of square matrix (n): ");
int n = [Link]();
int[][] matrix = new int[n][n];
[Link]("Enter matrix elements:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = [Link]();
}
}
boolean isSymmetric = true;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] != matrix[j][i]) {
isSymmetric = false;
break;
}
}
if (!isSymmetric) break;
}
if (isSymmetric) {
[Link]("The matrix is symmetric.");
} else {
[Link]("The matrix is not symmetric.");
}
[Link]();
}
}
Output:
Enter order of square matrix (n): 3
Enter matrix elements:
123
245
356
The matrix is symmetric.
3) write a program in java that takes input a range (m,n) and prints all the prime palindromic numbers within that
range.
Sol.
import [Link];
public class prime_palindrome {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter lower bound (m): ");
int m = [Link]();
[Link]("Enter upper bound (n): ");
int n = [Link]();
[Link]("Prime palindromic numbers between " + m + " and " + n
+ ":");
for (int i = m; i <= n; i++) {
if (isPrime(i) && isPalindrome(i)) {
[Link](i);
}
}
[Link]();
}
static boolean isPrime(int num) {
if (num < 2) return false;
for (int i = 2; i <= [Link](num); i++) {
if (num % i == 0) return false;
}
return true;
}
static boolean isPalindrome(int num) {
int original = num, reversed = 0;
while (num > 0) {
reversed = reversed * 10 + num % 10;
num /= 10;
}
return original == reversed;
}
}
Output:
Enter lower bound (m): 1
Enter upper bound (n): 2027
Prime palindromic numbers between 1 and 2027:2
3
5
7
11
101
131
151
181
191
313
353
373
383
727
757
787
797
919
929
4) Write a program in java to accept a word and insert it at a given input position in an input statement.
Sol.
import [Link];
public class insert_word {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a sentence: ");
String sentence = [Link]();
[Link]("Enter the word to insert: ");
String word = [Link]();
[Link]("Enter the position to insert the word (starting from
1): ");
int position = [Link]();
String result= "";
for (int i = 0; i < [Link](); i++) {
if (i == position - 1) {
result+= word+" ";
}
result+= [Link](i);
}
[Link]("Modified sentence: " + result);
[Link]();
}
}
Output:
Enter a sentence: I am boy
Enter the word to insert: a
Enter the position to insert the word (starting from 1): 6
Modified sentence: I am a boy
5) A special number is a number in which the sum of factorial of each digit is equal to the number itself. Ex- 145
1!+4!+5!= 145.
Design a class number to check if a given number is a special number or not. Some of the members of the class
are given below:-
Class name- Number
Data member: n:- integer
Member functions:
Number(): constructor to assign 0 to n
Number(int x): Parameterised constructor to assign x to n
int fact(int k): Calculate and return the factorial value of k
void isspecial(): check and display input is special or not.
Design a main function to call the above member functions.
Sol.
class Number {
int n;
// Default constructor
Number() {
n = 0;
}
// Parameterized constructor
Number(int x) {
n = x;
}
// Method to calculate factorial
int fact(int k) {
int f = 1;
for (int i = 2; i <= k; i++) {
f *= i;
}
return f;
}
// Method to check and display if number is special
void isspecial() {
int temp = n, sum = 0;
while (temp > 0) {
int digit = temp % 10;
sum += fact(digit);
temp /= 10;
}
if (sum == n) {
[Link](n + " is a special number.");
} else {
[Link](n + " is not a special number.");
}
}
}
// Main function to test the class
public static void main(String[] args) {
Number num1 = new Number(145);
[Link]();
Number num2 = new Number(123);
[Link]();
}
Output:
145 is a special number.
123 is not a special number.
6) Design a class Armstrong to check whether a given number is Armstrong or not.
An Armstrong number is a positive m digit number that is equal to the sum of the nth power of their digits. Ex-
1634= 1+1296+81+256=1634
Some of the members of the class are given below:
Class name- Armstrong
Data members: num (to store the number)
Member methods:
Void getnum- accepts number in num
int countdigit(int a)- counts and returns the number of digits in a
int superpower(int a, int p)- returns the sum of pth power of the digits of the number a
void isarmstrong()- uses the above functions and check if the number is armstorng or not.
Sol.
import [Link];
class Armstrong {
int num;
// Accepts number in num
void getnum() {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
num = [Link]();
[Link]();
}
// Counts and returns the number of digits in a
int countdigit(int a) {
int count = 0;
while (a > 0) {
count++;
a /= 10;
}
return count;
}
// Returns the sum of pth power of the digits of the number a
int superpower(int a, int p) {
int sum = 0;
while (a > 0) {
int digit = a % 10;
sum += [Link](digit, p);
a /= 10;
}
return sum;
}
// Checks if the number is Armstrong or not
void isarmstrong() {
int digits = countdigit(num);
int sum = superpower(num, digits);
if (sum == num) {
[Link](num + " is an Armstrong number.");
} else {
[Link](num + " is not an Armstrong number.");
}
}
}
// Main function to test the class
public class ArmstrongTest {
public static void main(String[] args) {
Armstrong obj = new Armstrong();
[Link]();
[Link]();
}
}
Output:
Enter a number:1634
1634 is an Armstrong number.
7) Write a program to input n integers in an array and place all the odd numbers in an ascending order followed by
all the even numbers in an ascending order.
Sol.
import [Link];
public class OddEvenSort {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of elements: ");
int n = [Link]();
int[] arr = new int[n];
int i, j;
[Link]("Enter " + n + " integers:");
for (i = 0; i < n; i++) {
arr[i] = [Link]();
}
// Separate odd and even numbers
int[] odd = new int[n];
int[] even = new int[n];
int oddCount = 0, evenCount = 0;
for (i = 0; i < n; i++) {
if (arr[i] % 2 != 0) {
odd[oddCount] = arr[i];
oddCount++;
} else {
even[evenCount] = arr[i];
evenCount++;
}
}
// Sort odd numbers
for (i = 0; i < oddCount - 1; i++) {
for (j = i + 1; j < oddCount; j++) {
if (odd[i] > odd[j]) {
int temp = odd[i];
odd[i] = odd[j];
odd[j] = temp;
}
}
}
// Sort even numbers
for (i = 0; i < evenCount - 1; i++) {
for (j = i + 1; j < evenCount; j++) {
if (even[i] > even[j]) {
int temp = even[i];
even[i] = even[j];
even[j] = temp;
}
}
}
// Print sorted array
[Link]("Sorted array (odds then evens):");
for (i = 0; i < oddCount; i++) {
[Link](odd[i] + " ");
}
for (i = 0; i < evenCount; i++) {
[Link](even[i] + " ");
}
[Link]();
}
}
Output:
Enter number of elements: 10
Enter 10 integers:
9261358241
Sorted array (odds then evens):
1135922468