[go: up one dir, main page]

0% found this document useful (0 votes)
63 views6 pages

Output:: Args Original Reverse in

Download as odt, pdf, or txt
Download as odt, pdf, or txt
Download as odt, pdf, or txt
You are on page 1/ 6

package com.

constructor;

import java.util.Scanner;
/**
*
* Palindrome class check string is palindrome or not.
*
*/
public class Palindrome
{
public static void main(String args[])
{
String original, reverse="";
Scanner in = new Scanner(System.in);

System.out.println("Enter a string to check if it is a palindrome");


original = in.nextLine();

int length = original.length();

for ( int i = length - 1 ; i >= 0 ; i-- ){


reverse = reverse + original.charAt(i);
}
if (original.equals(reverse)){
System.out.println("Entered string is a palindrome.");
}
else{
System.out.println("Entered string is not a palindrome.");
}

}
}

Output :
Enter a string to check if it is a palindrome
shweta
Entered string is not a palindrome.

Enter a string to check if it is a palindrome


madam
Entered string is a palindrome.

package com.project;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;
public class ISBNCode {

public static void main(String args[]) throws IOException{

InputStreamReader in= new InputStreamReader(System.in);

BufferedReader ob= new BufferedReader(in);

System.out.println("enter the ISBN code:");

String str = ob.readLine();

int len=str.length();

if(len!=10){

System.out.println("Invalid Input");

else {

int num=0;

int sum = 0;

String su = "0";

char[] chrs = str.toCharArray();

int i=1;

for(Character ch:chrs){

su = ch.toString();

num = Integer.parseInt(su)*(i);

sum = sum+num;

i++;

}
System.out.println("===sum is=="+sum);

if(sum%11==0){

System.out.println("Legal ISBN");

else{

System.out.println("Illegal ISBN");

Output:
enter the ISBN code:
0201103311
===sum is==77
Legal ISBN

package com.project;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Kaprekar {


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");
}

Output:
Enter a Number : 9
9 is a Kaprekar Number
package com.project;

public class Sorting {


public static void main(String[] args)
{
String[] arr = {"Banana", "Apple", "Orange", "Fruit", "Watermelon", "Hello
World"};
String tmp;
for (int i = 0;i < arr.length;i++)
{
tmp = arr[i];
for (int j = 0;j < arr.length;j++)
{
if (i == j) continue;
int x = tmp.compareTo(arr[j]);
if (x < 0)
{
tmp = arr[j];
arr[j] = arr[i];
arr[i] = tmp;
}
}
}
for (int i = 0;i < arr.length;i++)
System.out.println(arr[i]);
}

Output:
Apple
Banana
Fruit
Hello World
Orange
Watermelon

You might also like