Output: 16
Output: 16
Define a class which will generate a random number in between the range
low and high. Here, low and high are the two fields of the class and
randGen(low, high) is the method in the class to generate random number.
System.out.print(random);
}
}
Output:
16
2. Define a class Matrix2D which will include row, column and a 2D array of
integer numbers of size row×column as data. Define the methods for
matrix addition, subtraction and multiplication, where each method take
an argument as another matrix to do the operation with the current matrix
and return the resultant matrix. Also, define another method print() to
print the matrix it stores. Write a main class, where you create three
matrix object a, b and c of type Matrix2D and obtain result c = a.add(b)
for addition of two matrix, that is, c = a+b, etc.
}
}
}
}
import java.util.Scanner;
if (thirdTerm == x) {
System.out.println(count + 1);
} else {
System.out.println("Number doesn't belongs to
Fibonacci series");
}
}
Output:
else
return 1 / n + (harmonic(n - 1));
}
if (out == out_verify) {
System.out.println("Output = " + out + " is
matching!");
} else {
System.out.println("Not matching!");
}
}
}
Enter the value of n : 6 Output = 2.4499999999999997 is matching!
5. Read a string of any characters from the keyboard. Write a recursive
function to check if it is a palindrome or not. For example, MALAYALAM
is palindrome and PUPPY is not. Now, extend the same thing to check if a
sentence (i.e., collection of words) is a palindrome or not. For example,
MADAM TOP POT MADAM is palindrome sequence.
import java.util.Scanner;
public class program {
//Recursive function that checks
//whether the string is palindrome or not
static boolean checkPalindrome(String str, int s, int
e) {
if (s == e) // If there is only one character
return true;
// If first and last characters do not match
if ((str.charAt(s)) != (str.charAt(e)))
return false;
// If there are multiple characters, check if
// middle substring is also palindrome or not.
if (s < e + 1)
return checkPalindrome(str, s + 1, e - 1);
return true;
}
static boolean isPalindrome(String str) {
int n = str.length();
// If string is empty,then it is palindrome
if (n == 0)
return true;
return checkPalindrome(str, 0, n - 1);
}
// Driver Code
public static void main(String args[]) {
//Take input from the user
Scanner sc = new Scanner(System.in);
System.out.println("Enter the String :");
String str = sc.nextLine(); //Input the string
//Check whether palindrome or not
if (isPalindrome(str))
System.out.println(str + " is palindrome");
else
System.out.println(str + " is not a
palindrome");
}
}
Enter the String : MALAYALAM MALAYALAM is palindrome Enter the
String : PUPPY PUPPY is not a palindrome Enter the String : MADAM TOP
POT MADAM MADAM TOP POT MADAM is palindrome
The word obtained after swapping of letters are then undergo the following
conversions.