Karpekar no
import java.io.*;
class KaprekarNum
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a Number : ");
int n = Integer.parseInt(br.readLine()); //Inputting the number
int sq = n*n; //finding the square of the number
String s = Integer.toString(sq); //converting the square into a String
if(sq<=9)
s = "0"+s; //Adding a zero in the beginning if the square is of single digit
int l = s.length(); //finding the length (i.e. no. of digits in the square).
int mid = l/2; //finding the middle point
String left=s.substring(0,mid); //extracting the left digits from the square
String right=s.substring(mid); //extracting the right digits from the square
int x = Integer.parseInt(left); //converting the left String into Integer
int y = Integer.parseInt(right); //converting the right String into Integer
//if sum of left and right numbers is equal to the original number then it is a Kaprekar number
if(x+y == n)
System.out.println(n+" is a Kaprekar Number");
else
System.out.println(n+" is Not a Kaprekar Number");
}
}
Write a program in Java to store the number in 4x4 matrix in a double dimensional
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double[][] matrix = new double[4][4];
System.out.println("Enter numbers for 4x4 matrix: ");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
matrix[i][j] = scanner.nextDouble();
}
double highest = matrix[0][0];
double lowest = matrix[0][0];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (matrix[i][j] > highest) {
highest = matrix[i][j];
if (matrix[i][j] < lowest) {
lowest = matrix[i][j];
System.out.println("The highest number in the array = " + highest);
System.out.println("The lowest number in the array = " + lowest);
public class OddEven
{
public static void main(String[] args) {
int rows, cols, countOdd = 0, countEven = 0;
//Initialize matrix a
int a[][] = {
{4, 1, 3},
{3, 5, 7},
{8, 2, 6}
};
//Calculates number of rows and columns present in given matrix
rows = a.length;
cols = a[0].length;
//Counts the number of even elements and odd elements
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
if(a[i][j] % 2 == 0)
countEven++;
else
countOdd++;
}
}
System.out.println("Frequency of odd numbers: " + countOdd);
System.out.println("Frequency of even numbers: " + countEven);
}
}
Define a class to accept two strings of same length and
form a new word in such a way that, the first character of
the first word is followed by the first character of the
second word and so on.
Example :
Input string 1 – BALL
Input string 2 – WORD
OUTPUT : BWAOLRLD
import java.util.Scanner;
public class KboatStringMerge
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter String 1: ");
String s1 = in.nextLine();
System.out.println("Enter String 2: ");
String s2 = in.nextLine();
String str = "";
int len = s1.length();
if(s2.length() == len)
{
for (int i = 0; i < len; i++)
{
char ch1 = s1.charAt(i);
char ch2 = s2.charAt(i);
str = str + ch1 + ch2;
}
System.out.println(str);
}
else
{
System.out.println("Strings should be of same length");
}
}
}