Author

Author- Ram Ranjeet Kumar
Showing posts with label Java Programs. Show all posts
Showing posts with label Java Programs. Show all posts

Tuesday, October 11, 2022

Number To Word Java Program

Click in this link and Support me, any amount is appreciated: Support - https://razorpay.me/@codingmoments

 ----------------------------------------------------------------------------------

Number To Word Java Program

//2--Two

//12--Twelve
//126-- one hundred twenty six
package basic.NumberProgram;

import java.text.NumberFormat;
import java.util.Scanner;

public class NumberToWord {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter a number (4 digit only)");
        int n=sc.nextInt();
//        if(n>0 && n<=9999)//999//9999
        System.out.println(printWord(n));//-7686
//        else
//            System.out.println("Bad input");

    }
    public static String printWord(int n){//2//-45465
        String unique[]={"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve",
        "Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};

        String tens[]={"","","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninty"};

        if(n<0)
            return "Minus "+ printWord(-n);//-(-34212)//34212

        if(n<20)
        {return unique[n];}
        if(n<100)//n==50==Twenty Three
        {
            if(n==20||n==30||n==40||n==50||n==60||n==70||n==80||n==90)
                return tens[n/10];
            else
            return tens[n/10] +(n%10!=0?" ":"")+ unique[n%10];
        }
        if(n<1000)//654==Six hundred Fifty four//900
        {
            if(n==100||n==200||n==300||n==400||n==500||n==600||n==700||n==800||n==900)
                return printWord(n/100) +" Hundred";
            else
            return printWord(n/100) +" Hundred"+(n%100!=0?" ":"")+printWord(n%100);//54
        }
        if(n<100000)//2,022 --Two Thousand Twenty two        29,876 Twenty Nine Thousand Eight Hundred Seventy Six
        {
            if(n==1000||n==2000||n==3000||n==4000||n==5000||n==6000||n==7000||n==8000||n==9000
            ||n==10000||n==20000||n==30000||n==40000||n==50000||n==60000||n==70000||n==80000||n==90000)
                return printWord(n/1000)+ " Thousand";
            else
                return printWord(n/1000)+ " Thousand"+(n%1000!=0?" ":"")+printWord(n%1000);//876

        }
        if(n<10000000)//234,567--Two Lakh Thirty Four Thousnad Five Hundred Sixtry Seven
        {
            if(n==100000||n==200000||n==300000||n==400000||n==500000||n==600000||n==700000||n==800000||n==900000
                    ||n==1000000||n==2000000||n==3000000||n==4000000||n==5000000||n==6000000||n==7000000||n==8000000||n==9000000)
                return printWord(n/100000)+ " Lakh";
            else
                return printWord(n/100000)+ " Lakh"+(n%100000!=0?" ":"")+printWord(n%100000);//876

        }

        return null;

    }

}

Friday, June 19, 2020

Java Programming



Write the program to count the no of bits

 import java.util.Scanner; public class SetedNo {

 static int countbits(int n){ int count=0; while(n>0){

if(n%2==1){

count++;

}

 

n=n/2;

 }

return count;


}

public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("enter the no"); int n=sc.nextInt();

int s=countbits(n); System.out.println("the no of bits "+s);

}

 

}

 

 

Output enter the no 7

the no of bits 3

 

 

Write a program to check no is happy no or not and perform operations

 

import java.util.Scanner;

 public class HappyNos {

static boolean ishappy(int n)

{

while(n>9){

int sum=0; while(n!=0){

int r=n%10; sum=sum+r*r; n=n/10;

 

}

n=sum;

 

}

 

return n==1||n==7;

}

static void happyno(int n){ System.out.println("Happy nos are"); for(int i=1;i<=n;i++){

if(ishappy(i)){

System.out.print(i+ " ");

}


}

 

}

static int counthappy(int n){ int count=0;

for(int i=1;i<=n;i++){

 

if(ishappy(i)){

count++;

}

}

return count;

}

static int sumhappy(int n){ int sum=0;

for(int i=1;i<=n;i++){

 

if(ishappy(i)){

sum=sum+i;

}

}

return sum;

}

public static void main(String[] args) { Scanner sc=new Scanner(System.in);


System.out.println("Enter the no"); int n=sc.nextInt();

boolean b=ishappy(n); if(b)

System.out.println("Happy no");

 

else

System.out.println("Not happy no"); happyno(n);

int cnt=counthappy(n); System.out.println("\ntotal happy are "+cnt); int sm=sumhappy(n); System.out.println("total happy are "+sm);

}

 

}

  

output

Enter the no 21

Not happy no Happy nos are 1 7 10 13 19

total happy are 5


total happy are 50

 

 
 

Write a program to Convert binary to decimal

 import java.util.Scanner;

  

public class BinarytoDecimal { static int bintodec(int n){

int dec=0; int c=0; while(n!=0){

int r=n%10;

  

dec=dec+r*pow(2,c); c++;

n=n/10;

}

return dec;

}

 

static int pow(int n, int c) { int pw=1;

while(c>0)

{

 

pw=pw*n; c--;

}

return pw;

}

 

public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter the no "); int n=sc.nextInt();

int bd=bintodec(n); System.out.println("The no is = "+bd);

}

 

}

 

 

output:-

 

Enter the no 111

The no is = 7 as(2+4+1)

 

Write a program to convert Decimal to binary

import java.util.Scanner;

  

public class Decimaltobinary {

 

static String dectobin(int n){ String bin=""; while(n>0){

int r=n%2; bin=r+bin; n=n/2;

  

}

return bin;

}

public static void main(String[] args) {


Scanner sc=new Scanner(System.in); System.out.println("Enter the no"); int n=sc.nextInt();

String bin=dectobin(n);

System.out.println("the binary of "+n+" is "+bin);

 

}

 

}

 

 

output

Enter the no 13

the binary of 13 is 1101

 

 

Write a program to convert binary to Hexadecimal

 import java.util.Scanner;

 

public class DecimaltoHexa { static String dectohex(int n){

String hx="";


while(n>0){

int r=n%16; if(r<10){

hx=r+hx;

}

 

else

hx=(char)(r+55)+hx;

//hx=(char)(r+87)+hx; n=n/16;

}

return hx;

 

}

public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter the no"); int n=sc.nextInt();

String octa=dectohex(n);

System.out.println("the hexadecimal is = "+octa);

  

}

}


 

output

Enter the no 12

the hexadecimal is = C

 

 

write a program to convert decimal to octal

 

import java.util.Scanner;

 

public class DecimaltoOctal { static String dectooct(int n){

String oct="";

  

while(n>0){

int r=n%8; oct=r+oct; n=n/8;

  

}


return oct;

 

}

public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter the no"); int n=sc.nextInt();

String oct=dectooct(n);

System.out.println("the binary of "+n+" is "+oct);

 

}

}

 

 

o/p:-

 

 

 

Enter the no 12

the binary of 12 is 14

 

 

 

==============================


write a program to check no is even or odd without using loop

 

import java.util.Scanner;

//using String Array public class Evenodd {

public static void main(String[] args) { Scanner key=new Scanner(System.in);

System.out.println("Enter the integer : "); int no=key.nextInt();

String str[]={"Even","Odd"};

System.out.println(no+" is "+str[no%2]+" Number ");

}

  

}

 

 

o/p:-

Enter the integer : 4

4 is Even Number


Array Class

1d array

***************

 

write a program to perform sum of array and average of elements of array

 

package onedimensionarry; import java.util.Scanner;

 

 

public class ArraysumandAverage {

public static void main(String[] args) { Scanner sc=new Scanner(System.in);

System.out.println("Enter the size of array"); int n=sc.nextInt();

int arr[]=new int[n];

System.out.println("enter "+n+" Elements of Array "); for(int i=0;i<n;i++){

arr[i]=sc.nextInt();

}

double sum=0.0;


for(int i=0;i<arr.length;i++){ sum=sum+arr[i];

}

double avg =sum/arr.length;

System.out.println("sum of "+n+" arrays are = "+sum); System.out.println("Avrage of "+n+" arrays are = "+avg);

 

 

}

 

}

o/p:-

 

Enter the size of array 5

enter 5 Elements of Array 1 2 4 7 8

sum of 5 arrays are = 22.0 Avrage of 5 arrays are = 4.4


write a program to show entered elements of array

,find biggest elements and sum of the array and average of array

 

package onedimensionarry;

import java.util.Scanner;

public class ArrayOperation

{

//Sum of n elements of Array// double sumOfArray(int a[])

{

double sum=0.0;

for(int i=0;i<a.length;i++){ sum=sum+a[i];

}

return sum;

}

//Read the Array Elements// int[]readArr(){

Scanner sc=new Scanner(System.in);


System.out.println("Enter the size of array"); int n=sc.nextInt();

int ar[]=new int[n];

System.out.println("enter "+n+" Elements of Array"); for(int i=0;i<ar.length;i++){

ar[i]=sc.nextInt();

 

}

return ar;

 

}

//Display entered Array elements //

void dispArr(int ele[]){

for(int i=0;i<ele.length;i++){ System.out.print(ele[i]); if(i<ele.length-1){

System.out.print(",");

}

}

System.out.println();

}


//Get the Biggest Array elements//

public int getBiggest(int[]ar){ int big=ar[0];

for(int i=1;i<ar.length;i++){ if(ar[i]>big)

                                       big=ar[i];

}

return big;

}

 

public static void main(String[] args) {

  

ArrayOperation ao=new ArrayOperation(); int ar[]=ao.readArr(); System.out.print("Entered elemnts are\n"); ao.dispArr(ar);

int k=ao.getBiggest(ar); System.out.println("Biggest element is = "+k); double sum=ao.sumOfArray(ar);

double avg =sum/ar.length;


System.out.println("sum of "+ar.length+" arrays are = "+sum); System.out.println("Avrage of "+ar.length+" arrays are = "+avg);

 

}}

 o/p:-Enter the size of array 5

enter 5 Elements of Array 1 3 5 6 7

Entered elemnts are 1,3,5,6,7

Biggest element is = 7 sum of 5 arrays are = 22.0

Avrage of 5 arrays are = 4.4

 

 

write a program to find no of occurence of elements in an array

 package onedimensionarry;

 import java.util.Scanner;

public class CountEleArray {

static void countEle(int[]ar){

 int n=ar.length;

for (int i = 0; i < n; i++) { int count=1;

for (int j = i+1; j < n; j++) { if(ar[i]==ar[j]){

count++; ar[j]=ar[n-1]; n--;

j--;

}

}

 

System.out.println("no of occurence of "+ar[i]+"-

>"+count);

}

}

public static void main(String[] args) {

 

Scanner sc=new Scanner(System.in); System.out.println("Enter the size of array"); int n=sc.nextInt();

System.out.println("enter the elemts"); int arr[]=new int[n];

for (int i = 0; i < arr.length; i++) { arr[i]=sc.nextInt();

}

countEle(arr);

 

}

 

}

 

 

o/p:-

 

Enter the size of array 4

enter the elemts 1 3 5 6

no of occurence of 1->1 no of occurence of 3->1 no of occurence of 5->1


no of occurence of 6->1

 

 

 

write a program to count no of prime elements in array

 

package onedimensionarry; import java.util.Scanner;

 

 

public class CountPrime {

int countPrime(int a[]){ int pc=0;

for (int i = 0; i < a.length; i++) { if(isprime(a[i]))

pc++;}

 

return pc;

}

  

static boolean isprime(int n)       { int i=2;

while(i<=n/2){

if(n%i==0)


return false;

 

i++;

 

}

return true;

 

}

 

public static void main(String[] args) { CountPrime pc=new CountPrime(); Scanner sc=new Scanner(System.in);

System.out.println("Enter the no of array"); int n=sc.nextInt(); System.out.println("Enter the elements"); int ar[]=new int [n];

for (int i = 0; i < ar.length; i++) { ar[i]=sc.nextInt();

 

}

int count=pc.countPrime(ar);

System.out.println("no of prime element is "+count);

}

 

}


o/p:-

 

Enter the no of array 4

Enter the elements 1 4 6 7

no of prime element is 2

 

 

 

Write a program to count no of even and odd

 

package onedimensionarry; import java.util.Scanner;

 

public class EvenOddCount1 {

 

 

int[] readArr()

{

 

 

Scanner sc=new Scanner(System.in);

System.out.println("Enter How many integer value you want"); int n=sc.nextInt();

int arr[]=new int[n]; System.out.println("enter the values"); for(int i=0;i<arr.length;i++)


{

 

arr[i]=sc.nextInt();

 

}

 

 

return arr;

 

}

 

 

void dispArr(int ele[])

{

for(int i=0;i<ele.length;i++)

{

System.out.print(ele[i]); if(i<ele.length-1)

System.out.print(",");

 

}

}

 

 

int[] countEvOd(int a[])

{

int count[]=new int[2]; System.out.println("even and odd values are "); for(int i=0;i<a.length;i++)


{

 

count[a[i]%2]++;

 

}

return count;

 

}

 

 

public static void main(String[] args) { EvenOddCount1 eo=new EvenOddCount1(); int arr[]=eo.readArr();

int[] evod=eo.countEvOd(arr); eo.dispArr(evod);

}

 

}

 

 

o/p:-

 

Enter How many integer value you want 3

enter the values 2 7 8

even and odd values are 2,1

 

 

write a program to find smallest elements in array


 

package onedimensionarry; import java.util.Scanner;

 

 

public class SmallArray {

//Read the Array Elements// int[]readArr(){

Scanner sc=new Scanner(System.in); System.out.println("Enter the size of array"); int n=sc.nextInt();

int ar[]=new int[n];

System.out.println("enter "+n+" Elements of Array "); for(int i=0;i<ar.length;i++){

ar[i]=sc.nextInt();

}

 

return ar;

 

 

}

 

//Display entered Array elements //

 

 

void dispArr(int ele[]){

for(int i=0;i<ele.length;i++){


System.out.print(ele[i]); if(i<ele.length-1){

System.out.print(",");

}

 

}

 

System.out.println();

 

}

public int getsmallest(int[]ar){ int small=ar[0];

for(int i=1;i<ar.length;i++){ if(ar[i]<small)

small=ar[i];

 

 

}

 

return small;

}

public static void main(String[] args) { SmallArray ao=new SmallArray(); int ar[]=ao.readArr();

System.out.print("Entered elemnts are "); ao.dispArr(ar);

int k=ao.getsmallest(ar);


System.out.println("Smallest element is "+k);

 

}

}

 

 

 

 

o/p:-

 

Enter the size of array 4

enter 4 Elements of Array 1 4 6 7

Entered elemnts are 1,4,6,7 Smallest element is 1

 

 

write a program to reverse array element

 

package onedimensionarry; import java.util.Scanner;

 

 

public class ReverseArray {

void reversearray(int ar[]){

for(int i=0;i<=ar.length/2;i++){ int t=ar[i]; ar[i]=ar[ar.length-1-i];


ar[ar.length-1-i]=t;

 

}

System.out.println("reverse elements"); for (int i = 0; i < ar.length; i++) {

System.out.print(ar[i]+" ");

 

 

}

 

 

 

}

public static void main(String[] args) { ReverseArray rc=new ReverseArray(); Scanner sc=new Scanner(System.in); System.out.print("Entered elemnts are "); int n=sc.nextInt(); System.out.println("Enter the elements"); int ar[]=new int [n];

for (int i = 0; i < ar.length; i++) { ar[i]=sc.nextInt();

 

}

rc.reversearray(ar);

}


}

 

 

o/p:-

 

Entered elemnts are 4 Enter the elements

2 4 6 7

 

reverse elements 7 4 6 2

 

 

Write a program to count no of positive and negative elements of array

 

 

package onedimensionarry; import java.util.Scanner;

 

public class NoOfpositive {

 

 

public int getpositive(int[]ar){ int count=0;

for(int i=0;i<ar.length;i++){ if(ar[i]>0)

count++;


 

 

}

return count;

 

}

public int getnegative(int[]arr){ int count1=0;

for(int i=0;i<arr.length;i++){ if(arr[i]<0)

count1++;

 

 

}

return count1;

}

public static void main(String[] args) { NoOfpositive ao=new NoOfpositive(); Scanner sc=new Scanner(System.in);

System.out.print("enter the no of elements of array"); int n=sc.nextInt();

System.out.println("Enter the elements"); int ar[]=new int [n];

for (int i = 0; i < ar.length; i++) { ar[i]=sc.nextInt();


 

 

}

 

 

int count=ao.getpositive(ar); System.out.println("Positive element is "+count); int count1=ao.getnegative(ar); System.out.println("Negative element is "+count1);

}

 

}

 

 

o/p:-

enter the no of elements of array6 Enter the elements

2 4 6 8 -3 -5

 

Positive element is 4 Negative element is 2

 

Write a program to insert element at certain position

 

package onedimensionarry; import java.util.Scanner;


public class InsertEle {

static int[] insert(int arr[],int ele,int index){ if(index<0||index>arr.length){

System.out.println("index not in range"); return arr;

}

int na[]=new int[arr.length+1]; na[index]=ele;

for(int i=0;i<arr.length;i++){ if(i<index)

na[i]=arr[i];

else

na[i+1]=arr[i];

}

 

return na;

}

public static void main(String[] args) { InsertEle ins=new InsertEle(); Scanner sc=new Scanner(System.in);

System.out.println("Enter the array size"); int n=sc.nextInt(); System.out.println("Enter the element");


int arr[]=new int[n];

for (int i = 0; i < arr.length; i++) { arr[i]=sc.nextInt();

}

System.out.println("enter element"); int ele=sc.nextInt(); System.out.println("enter position"); int index=sc.nextInt(); arr=insert(arr,ele,index);

sc.close();

System.out.println("The new elemnts"); for (int i = 0; i < arr.length; i++) {

System.out.print(arr[i]+" ");

 

 

}

 

}

 

 

 

}

o/p:-

 

Enter the array size 4

Enter the element


1 5 7 9

 

enter element 6

enter position 3

The new elemnts 1 5 7 6 9

 

 

Write a program to delete an element at certain posn

 

package onedimensionarry;

 

 

import java.util.Scanner;

 

 

public class Delete {

static int[] delete(int arr[],int index){ if(index<0||index>arr.length){

System.out.println("index not in range"); return arr;

}

 

int na[]=new int[arr.length-1];

 

 

for(int i=0;i<na.length;i++){


if(i<index)

na[i]=arr[i];

else

na[i]=arr[i+1];

 

}

 

return na;

 

}

public static void main(String[] args) {

 

 

Scanner sc=new Scanner(System.in); System.out.println("Enter the array size"); int n=sc.nextInt(); System.out.println("Enter the element"); int arr[]=new int[n];

for (int i = 0; i < arr.length; i++) { arr[i]=sc.nextInt();

}

 

 

System.out.println("Enter the position"); int index=sc.nextInt(); arr=delete(arr,index);

sc.close();


System.out.println("The new elemnts"); for (int i = 0; i < arr.length; i++) { System.out.print(arr[i]+" ");

 

 

}

 

}

 

}

 

 

o/p:-

Enter the array size 3

Enter the element 2 5 7

Enter the position 1

The new elemnts 2 7

 

Write a program to find first two biggest element

 

package onedimensionarry;


import java.util.Scanner;

 

 

public class FirsttwoBiggest {

 

 

public static void main(String[] args) {

 

 

Scanner sc=new Scanner(System.in); System.out.println("Enter the array size"); int n=sc.nextInt(); System.out.println("Enter the element"); int arr[]=new int[n];

for (int i = 0; i < arr.length; i++) { arr[i]=sc.nextInt();

}

 

 

int h1=arr[0]; int h2=arr[1];

for (int i = 0; i < arr.length; i++) { if(h1<arr[i]){

h2=h1; h1=arr[i];

}


else if(arr[i]>h2&&h1!=arr[i]){ h2=arr[i];

}

}

System.out.println("1st biggest = "+h1); System.out.println("2nd biggest = "+h2);

}

}

 

 

o/p:-

Enter the array size 5

Enter the element 1 4 6 8 9

1st biggest = 9 2nd biggest = 8

 

Write a program to find two least elements of array

 

 

package onedimensionarry; import java.util.Scanner;


 

 

public class FirsttwoLeast {

public static void main(String[] args) {

 

 

Scanner sc=new Scanner(System.in); System.out.println("Enter the array size"); int n=sc.nextInt(); System.out.println("Enter the element"); int arr[]=new int[n];

for (int i = 0; i < arr.length; i++) { arr[i]=sc.nextInt();

}

 

 

int l1=arr[0]; int l2=arr[1];

for (int i = 0; i < arr.length; i++) { if(l1>arr[i]){

l2=l1; l1=arr[i];

}

else if(arr[i]<l2&&l1!=arr[i]){ l2=arr[i];


}

 

}

 

 

System.out.println("1st least = "+l1); System.out.println("2nd least = "+l2);

}

 

}

 

 

o/p:-

Enter the array size 4

Enter the element 2 7 1 9

1st least = 1 2nd least = 2

 

Write   a   program  to   count   no   of   element  in   array without array

 

package onedimensionarry; import java.util.Scanner;


public class WithoutCompareCountEle { public static void main(String[] args) {

 

Scanner sc=new Scanner(System.in); System.out.println("Enter the size of array"); int n=sc.nextInt();

System.out.println("enter the elemts"); int arr[]=new int[n];

for (int i = 0; i < arr.length; i++) { arr[i]=sc.nextInt();

 

 

 

}

countElement(arr);

 

 

 

 

 

 

}

static void countElement(int arr[]){ int big=arr[0];

for(int i=0;i<arr.length;i++){ if(big<arr[i])


big=arr[i];

 

}

int c[]=new int [big+1];

for (int i = 0; i < arr.length; i++) { c[arr[i]]++;

}

 

for(int i =0;i<c.length;i++){ if(c[i]>0)

System.out.println(i+"-->"+c[i]);

}

 

}

 

}

 

 

o/p:- 4

enter the elemts 1 5 86

7

 

1-->1

5-->1

7-->1

86-->1


 

 

Write a program to perform union operation with two two array

 

package onedimensionarry; import java.util.LinkedHashSet; import java.util.Scanner; import java.util.Set;

 

public class UnionOperation {

 

 

static int[] union(int a[],int b[]){

Set<Integer> us=new LinkedHashSet<Integer>(); for (int i = 0; i < a.length; i++) {

us.add(a[i]);

}

for(int i=0;i<b.length;i++){ us.add(b[i]);

}

int ar[]=new int[us.size()]; int i=0;

for(Integer ele:us){


ar[i]=ele; i++;

}

return ar;

 

 

 

}

 

 

 

 

 

 

 

 

 

public static void main(String[] args) { UnionOperation un=new UnionOperation(); Scanner sc=new Scanner(System.in);

System.out.println("enter the no of value for 1st array"); int n1=sc.nextInt();

int a[]=new int[n1]; System.out.println("enter the value"); for (int i = 0; i < a.length; i++) {

a[i]=sc.nextInt();

 

 

}


System.out.println("enter the no of value for 2nd array"); int n2=sc.nextInt();

int b[]=new int[n2]; System.out.println("enter the value"); for (int i = 0; i < b.length; i++) {

b[i]=sc.nextInt();

 

 

}

 

 

//int ar[]=new int[a.length+b.length]; int [] ar=union(a,b); System.out.println("the result array is"); for (int i = 0; i < ar.length; i++) {

System.out.print(ar[i]+" ");

 

 

}

 

}

 

 

 

}

o/p:-

enter the no of value for 1st array 4


enter the value 1 3 5 6

enter the no of value for 2nd array 3

enter the value 4 6 8

the result array is 1 3 5 6 4 8

 

Write a program to perform minus operation

 

package onedimensionarry; import java.util.LinkedHashSet; import java.util.Scanner; import java.util.Set;

 

public class Minus {

 

 

static int[] Minus(int a[],int b[]){

Set<Integer> ms=new LinkedHashSet<Integer>(); int[] ar=new int[a.length+b.length];

for (int i = 0; i < a.length; i++) {


int f=1;

for(int j=0;j<b.length;j++)

{

if(a[i]==b[j]){

f=0;

 

break;

}

}

if(f==1)

ms.add(a[i]);

 

}

int c[]=new int [ms.size()]; int i=0;

for(Integer in:ms){ c[i]=in;

i++;

}

return c;

 

 

 

}

public static void main(String[] args) { InterSection un=new InterSection();


Scanner sc=new Scanner(System.in); System.out.println("enter the no of value for 1st array"); int n1=sc.nextInt();

int a[]=new int[n1]; System.out.println("enter the value"); for (int i = 0; i < a.length; i++) {

a[i]=sc.nextInt();

 

 

}

System.out.println("enter the no of value for 2nd array"); int n2=sc.nextInt();

int b[]=new int[n2]; System.out.println("enter the value"); for (int i = 0; i < b.length; i++) {

b[i]=sc.nextInt();

 

 

}

int [] ar1=Minus(a,b);

System.out.println("the result array (a-b) is"); for (int i = 0; i < ar1.length; i++) {

if(ar1[i]!=0)

System.out.println(ar1[i]+" ");


 

 

}

System.out.println("====================="); int [] ar2=Minus(b,a);

System.out.println("the result array (b-a) is"); for (int i = 0; i < ar2.length; i++) {

if(ar2[i]!=0)

System.out.print(ar2[i]+" ");

 

 

}

sc.close();

 

}

 

}

 

 

o/p:-

enter the no of value for 1st array 3

enter the value 1 5 7

enter the no of value for 2nd array 5

enter the value


1 4 8 4 5

the result array (a-b) is 7

=====================

the result array (b-a) is 4 8

 

Write a program to perform intersection operation

 

package onedimensionarry; import java.util.LinkedHashSet; import java.util.Scanner; import java.util.Set;

 

 

public class InterSection {

static int[] interSection(int a[],int b[]){ int[] ar=new int[a.length+b.length]; for (int i = 0; i < a.length; i++) {

for(int j=0;j<b.length;j++){

 

 

if(a[i]==b[j])

ar[i]=a[i];


}

 

}

return ar;

 

 

 

}

 

static int[] interSection2(int a[],int b[]){

 

Set<Integer> is=new LinkedHashSet<Integer>(); int[] ar=new int[a.length+b.length];

for (int i = 0; i < a.length; i++) { for(int j=0;j<b.length;j++){

 

 

if(a[i]==b[j])

is.add(a[i]);

}

 

}

int c[]=new int [is.size()]; int i=0;

for(Integer in:is){

 

c[i]=in; i++;

}

return c;


 

 

}

public static void main(String[] args) { InterSection un=new InterSection(); Scanner sc=new Scanner(System.in);

System.out.println("enter the no of value for 1st array"); int n1=sc.nextInt();

int a[]=new int[n1]; System.out.println("enter the value"); for (int i = 0; i < a.length; i++) {

a[i]=sc.nextInt();

 

 

}

System.out.println("enter the no of value for 2nd array"); int n2=sc.nextInt();

int b[]=new int[n2]; System.out.println("enter the value"); for (int i = 0; i < b.length; i++) {

b[i]=sc.nextInt();

 

 

}

int [] ar1=interSection2(a,b);


System.out.println("the result array is"); for (int i = 0; i < ar1.length; i++) {

if(ar1[i]!=0)

System.out.print(ar1[i]+" ");

 

 

}

sc.close();

 

}

 

}

 

 

o/p:-

enter the no of value for 1st array 4

enter the value 3 5 7 8

enter the no of value for 2nd array 4

enter the value 4 2 5 3

the result array is 3 5


Write a program to merge two array

package onedimensionarry; import java.util.Scanner;

 

 

public class MergeArray {

 

 

public static int[]merge(int[]x,int[]y){

int rs[]=new int[x.length+y.length]; for(int i=0;i<x.length;i++){

rs[i]=x[i];

 

 

}

 

for(int i=0;i<y.length;i++){ rs[x.length+i]=y[i];

}

return rs;

}

 

 

int[]readArr(){

Scanner sc=new Scanner(System.in); System.out.println("Enter the size of array");


int n=sc.nextInt(); int ar[]=new int[n];

System.out.println("enter Elements of Array "); for(int i=0;i<ar.length;i++){

ar[i]=sc.nextInt();

 

}

return ar;

 

 

 

}

//Display entered Array elements //

 

 

void dispArr(int ele[]){

for(int i=0;i<ele.length;i++){ System.out.print(ele[i]); if(i<ele.length-1){

System.out.print(",");

}

}

System.out.println();

}


 

 

public static void main(String[] args) { MergeArray ma=new MergeArray();

 

 

int a[]=ma.readArr(); System.out.print("Entered elements are :"); ma.dispArr(a);

int b[]=ma.readArr(); System.out.print("Entered elements are :"); ma.dispArr(b);

System.out.println("Merged values are :"); int c[]=ma.merge(a, b);

ma.dispArr(c);

 

 

}

 

}

 

 

o/p:-

Enter the size of array 3

enter Elements of Array 1 2 4


Entered elements are :1,2,4 Enter the size of array

3

enter Elements of Array 4 5

6

 

Entered elements are :4,5,6 Merged values are :

1,2,4,4,5,6

 

 

write a program for zigzag merge of two array

 

package onedimensionarry; import java.util.Scanner;

 

public class MergeArray {

 

 

public static int[]merge(int[]x,int[]y){

int rs[]=new int[x.length+y.length]; for(int i=0;i<x.length;i++){

rs[i]=x[i];


}

for(int i=0;i<y.length;i++){ rs[x.length+i]=y[i];

}

return rs;

 

}

 

 

int[]readArr(){

Scanner sc=new Scanner(System.in); System.out.println("Enter the size of array"); int n=sc.nextInt();

int ar[]=new int[n];

System.out.println("enter Elements of Array "); for(int i=0;i<ar.length;i++){

ar[i]=sc.nextInt();

}

return ar;

 

 

}

//Display entered Array elements //

 

 

void dispArr(int ele[]){


for(int i=0;i<ele.length;i++){ System.out.print(ele[i]); if(i<ele.length-1){

System.out.print(",");

}

 

}

System.out.println();

 

}

 

 

 

 

 

 

public static void main(String[] args) { MergeArray ma=new MergeArray();

 

int a[]=ma.readArr(); System.out.print("Entered elements are :"); ma.dispArr(a);

int b[]=ma.readArr(); System.out.print("Entered elements are :"); ma.dispArr(b);

System.out.println("Merged values are :"); int c[]=ma.merge(a, b);


ma.dispArr(c);

 

 

 

}

 

}

 

 

o/p:-

 

Enter the size of array 4

enter Elements of Array 2 3 4 5

Entered elements are :

2,3,4,5

Enter the size of array 3

enter Elements of Array 1 2 3

Entered elements are :

1,2,3

 

merged values are 2,3,4,5,1,2,3

 

Write the program to find biggest elements from array


 

package onedimensionarry;

 

 

import java.util.Scanner;

 

 

 

 

 

 

public class NBiggest {

static int nthBiggest(int a[]){ int big=a[0];

for (int i = 1; i < a.length; i++) { if( a[i]>big){

return big=a[i];

}

}

 

return big;

 

 

}

public static void main(String[] args) { Scanner sc=new Scanner(System.in);

System.out.println("Enter the no of array"); int n=sc.nextInt();


System.out.println("Enter the elements"); int ar[]=new int [n];

for (int i = 0; i <ar.length; i++) { ar[i]=sc.nextInt();

}

 

int value=nthBiggest(ar); System.out.println("biggest "+value);

}

 

}

 

 

o/p:-

Enter the no of array 5

Enter the elements 98 1 77 888 8

biggest 888

 

 

 

 

2d-array

************

Write a program to reverse elements of matrix


package twodimensional;

 

 

import java.util.Scanner;

 

 

public class Rowreverse {

 

//rowwise reverse

static void rowreverse(int ar[][]){ for(int i=0;i<ar.length;i++){

for (int j = 0; j < ar[i].length/2; j++) { int t=ar[i][j]; ar[i][j]=ar[i][ar[i].length-1-j];

ar[i][ar[i].length-1-j]=t;

 

 

}

 

}

for (int i = 0; i < ar.length; i++) {

for (int j = 0; j < ar.length; j++) { System.out.print(ar[i][j]+" ");

 

 

}

System.out.println();

}


}

 

 

//column reverse

 

 

static void columnreverse(int arr[][]){ for(int i=0;i<arr.length/2;i++){

for (int j = 0; j < arr[i].length; j++) {

 

 

int t=arr[i][j]; arr[i][j]=arr[arr.length-1-i][j]; arr[arr.length-1-i][j]= t;

 

 

}

}

 

for (int i = 0; i < arr.length; i++) {

for (int j = 0; j < arr.length; j++) { System.out.print(arr[i][j]+" ");

 

 

}

System.out.println();

}

}


public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("enter the array"); int row=sc.nextInt();

int column=sc.nextInt(); System.out.println("enter the elements"); int arr[][]=new int[row][column];

for (int i = 0; i < arr.length; i++) {

for (int j = 0; j < arr[i].length; j++) { arr[i][j]=sc.nextInt();

 

 

}

 

 

}

 

System.out.println("Row Reverse"); rowreverse(arr); System.out.println("column Reverse"); columnreverse(arr);

}

}


o/p:-enter the array 2

2

enter the elements 22 33

44 55

 

Row Reverse 33 22

55 44

column Reverse 55 44

33 22

 

 

Write a program to find sum of Matrix

 

package twodimensional;

 

 

import java.util.Scanner;

 

 

public class Summatrix { static int[][]readmat(){

Scanner sc=new Scanner(System.in);


System.out.println("enter row and column"); int r=sc.nextInt();

int c=sc.nextInt();

int mat[][]=new int[r][c];

for (int i = 0; i < mat.length; i++) {

 

//System.out.println("enter"+i+1+"row elements"); for (int j = 0; j < mat[i].length; j++) {

 

mat[i][j]=sc.nextInt();

}

 

 

}

 

return mat;

 

 

 

}

//display 2-d from user static void disMat(int[][]x)

{

 

for (int i = 0; i < x.length; i++) {

for (int j = 0; j < x[i].length; j++) {

 

 

System.out.print(x[i][j]+" ");


}

System.out.println();

 

}

 

}

static int[][] summat(int a[][],int b[][] ){ if(a.length!=b[0].length || a[0].length!=b.length){

System.out.println("next elements"); return null;

 

 

}

int p[][]=new int[a.length][b[0].length]; for (int i = 0; i < a.length; i++) {

for (int j = 0; j < b[i].length; j++) {

for (int k= 0; k < b.length; k++) { p[i][j]=a[i][j]+b[i][j];

 

}

 

 

}

 

 

}

return p;


}

public static void main(String[] args) { Summatrix p=new Summatrix(); System.out.println("enter 1st matrix"); int[][] m1=p.readmat(); System.out.println("1st matrix is "); p.disMat(m1); System.out.println("enter 2nd matrix"); int[][] m2=p.readmat(); System.out.println("2nd matrix is : "); p.disMat(m2);

System.out.println("Sum of matrix is :");

 

int res[][]=p.summat(m1, m2); p.disMat(res);

 

 

}

 

 

 

}

 

 

o/p:-enter 1st matrix enter row and column 2 2


2 4

3 5

1st matrix is 2 4

3 5

 

enter 2nd matrix enter row and column 2 2

3 6

7 5

2nd matrix is :

3 6

7 5

Sum of matrix is :

 

5 10

10 10

 

 

Write a program to find product of matrix

 

 

package twodimensional;


import java.util.Scanner;

 

 

public class ProductTwomatrix { static int[][]readmat(){

Scanner sc=new Scanner(System.in); System.out.println("enter row and column"); int r=sc.nextInt();

int c=sc.nextInt();

int mat[][]=new int[r][c];

for (int i = 0; i < mat.length; i++) {

//System.out.println("enter"+i+1+"row elements"); for (int j = 0; j < mat[i].length; j++) {

 

 

mat[i][j]=sc.nextInt();

 

}

 

 

}

return mat;

 

 

}

//display 2-d from user static void disMat(int[][]x)


{

 

for (int i = 0; i < x.length; i++) {

for (int j = 0; j < x[i].length; j++) {

 

 

System.out.print(x[i][j]+" ");

 

}

System.out.println();

}

 

}

static int[][] productmat(int a[][],int b[][] ){ if(a.length!=b[0].length || a[0].length!=b.length){

System.out.println("next elements"); return null;

 

}

int p[][]=new int[a.length][b[0].length]; for (int i = 0; i < a.length; i++) {

for (int j = 0; j < b[i].length; j++) {

for (int k= 0; k < b.length; k++) { p[i][j]=p[i][j]+a[i][j]*b[k][j];

 

 

}


 

 

}

 

 

 

}

return p;

 

}

public static void main(String[] args) { ProductTwomatrix p=new ProductTwomatrix(); System.out.println("enter 1st matrix");

int[][] m1=p.readmat(); p.disMat(m1);

System.out.println("enter 2nd matrix"); int[][] m2=p.readmat();

p.disMat(m2);

 

System.out.println("Display multiplication"); int res[][]=p.productmat(m1, m2); p.disMat(res);

 

 

}

 

 

 

}


o/p:-enter 1st matrix enter row and column 2 2

3 4

4 5

 

3 4

4 5

enter 2nd matrix enter row and column 2 2

3 5

6 7

3 5

6 7

 

Display multiplication 27 48

36 60

 

 

write a program to rotate and transpose a matrix

 

 

package twodimensional;


 

 

import java.util.Scanner;

 

 

public class RotateandTranspose { static void transpose(int arr[][]){

for (int i = 0; i < arr.length; i++) {

for (int j = 0; j < arr[i].length; j++) {

 

 

int t=arr[i][j]; arr[i][j]=arr[j][i]; arr[j][i]=t;

}

 

 

}

 

for (int i = 0; i < arr.length; i++) {

for (int j = 0; j < arr.length; j++) { System.out.print(arr[i][j]+" ");

 

 

}

System.out.println();

}

}


static void columnreverse(int arr[][]){ for(int i=0;i<arr.length/2;i++){

for (int j = 0; j < arr[i].length; j++) {

 

 

int t=arr[i][j]; arr[i][j]=arr[arr.length-1-i][j]; arr[arr.length-1-i][j]= t;

 

}

}

for (int i = 0; i < arr.length; i++) {

for (int j = 0; j < arr.length; j++) { System.out.print(arr[i][j]+" ");

 

}

System.out.println();

}

}

 

 

static void rowreverse(int ar[][]){ for(int i=0;i<ar.length;i++){

for (int j = 0; j < ar[i].length/2; j++) {


int t=ar[i][j]; ar[i][j]=ar[i][ar[i].length-1-j];

ar[i][ar[i].length-1-j]=t;

 

}

 

}

 

for (int i = 0; i < ar.length; i++) {

for (int j = 0; j < ar.length; j++) { System.out.print(ar[i][j]+" ");

 

 

}

System.out.println();

}

 

}

static void rotate90left(int arr[][]){ transpose(arr);

System.out.println("rotate 90 left Matrix"); columnreverse(arr); System.out.println("=====================");

}

 

 

static void rotate90right(int arr[][]){ transpose(arr);


System.out.println("rotate 90 right Matrix"); rowreverse(arr);

}

 

 

 

 

 

 

public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("enter the array"); int row=sc.nextInt();

int column=sc.nextInt(); System.out.println("enter the elements"); int arr[][]=new int[row][column];

for (int i = 0; i < arr.length; i++) {

 

for (int j = 0; j < arr[i].length; j++) { arr[i][j]=sc.nextInt();

 

 

}

 

 

}

rotate90left(arr); rotate90right(arr);


}

 

}

 

 

 

 

o/p:-

 

enter the array 2 2

enter the elements 3 65

7 9

3 65

7 9

rotate 90 left Matrix 7 9

3 65

===================== 7 9

3 65

rotate 90 right Matrix 9 7

65 3


Write a program to rotate diagonal of matrix

 

package twodimensional;

 

 

import java.util.Scanner;

 

 

public class Rotatedigonal {

static void reverseDigonal(int arr[][]){ for (int i = 0; i < arr.length/2; i++) {

for (int j = 0; j < arr[i].length; j++) {

 

 

 

 

if(i==j){

 

int t=arr[i][j];

arr[i][j]=arr[arr.length-1-i][arr.length-1-j]; arr[arr.length-1-i][arr.length-1-j]=t;

}

if(i+j==arr.length-1){

int t=arr[i][j]; arr[i][j]=arr[j][i]; arr[i][j]=t;

}


}

}

 

for (int i = 0; i < arr.length; i++) {

for (int j = 0; j < arr.length; j++) { System.out.print(arr[i][j]+" ");

 

 

}

System.out.println();

}

 

 

 

}

public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("enter the array"); int row=sc.nextInt();

int column=sc.nextInt(); System.out.println("enter the elements"); int arr[][]=new int[row][column];

for (int i = 0; i < arr.length; i++) {

for (int j = 0; j < arr[i].length; j++) { arr[i][j]=sc.nextInt();


}

 

 

 

}

System.out.println("Rotate diagonal value"); reverseDigonal(arr);

}

}

 

 

o/p:-

enter the array 2 2

enter the elements 4 8

8 9

 

Rotate diagonal value 9 8

8 4

 

 

Write a program to find spiral of matrix

 

//java program to matrix element into spiral elements//


package twodimensional;

 

 

import java.util.Scanner;

 

 

public class SpiralMatrix {

 

static void spiralmat(int [][] ar){

//int n=5;

for (int i = 0,j=ar.length-1; i < j; i++,j--) { for (int k = i; k < j; k++) {

System.out.print(ar[i][k]+" ");

}

System.out.println(); for (int k = i; k < j; k++) {

System.out.print(ar[k][j]+" ");

 

}

System.out.println(); for (int k = j; k >i; k--) {

System.out.print(ar[j][k]+" ");

 

 

}

System.out.println(); for(int k=j;k>i;k--){


System.out.print(ar[k][i]+" ");

 

}

System.out.println(); if(ar.length%2==1)

System.out.print(ar[ar.length/2][ar.length/2]);

 

}

 

}

 

 

 

 

public static void main(String[] args) { SpiralMatrix s=new SpiralMatrix(); Scanner sc=new Scanner(System.in); System.out.println("enter the array"); int row=sc.nextInt();

int column=sc.nextInt(); System.out.println("enter the elements"); int ar[][]=new int[row][column];

for (int i = 0; i < ar.length; i++) {

for (int j = 0; j < ar[i].length; j++) { ar[i][j]=sc.nextInt();

 

 

}


 

 

}

System.out.println("Spiral matrix"); s.spiralmat(ar);

}

 

 

 

}

 

 

o/p:-

enter the array 2 2

enter the elements 7 8

5 9

 

Spiral matrix 7

8

9

5

 

 

Write a program to find biggest shape

================


Shape interface

================

 

package twodimensional;

 

 

public interface Shape { double getArea();

}

 

 

================

 

 

Circle class

================

 

package twodimensional;

 

 

public class Circle implements Shape

{

double r;

 

 

 

 

public Circle(double r) {

 

 

this.r = r;


}

 

 

@Override

public double getArea() { return 3.14*r*r;

}

 

@Override

public String toString() {

return "Circle [radius=" + r+" Area= "+getArea() + "]";

}

 

 

 

}

 

 

=================

 

 

Reactangle class

=================

 

 

 

package twodimensional;

 

 

public class Reactangle implements Shape { double l;

double b;


 

 

public Reactangle(double l, double b) {

 

 

this.l = l; this.b = b;

}

 

 

@Override

public double getArea() { return 2*l*b;

 

 

}

@Override

public String toString() {

return "Reactangle [length =" + l + ", breath =" + b +" Area = "+getArea() + "]";

}

 

 

}

 

Square class

==================


package twodimensional;

 

 

public class Square implements Shape { double side;

 

 

public Square(double side) {

 

 

this.side = side;

}

 

 

@Override

public double getArea() { return side*side;

 

}

@Override

public String toString() {

return "Square [ side = " + side+" Area = "+getArea() + "]";

 

}

 

 

}


mainrunner class

=================

 

 

 

package twodimensional;

//java program to read store shape info and display biggest area shapeinfo.//

 

 

import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Scanner;

 

 

public class MainRunner {

static ArrayList<Shape> getShapes(){

 

ArrayList<Shape> shapes=new ArrayList<Shape> (); Scanner sc=new Scanner(System.in);

 

while(true){

Shape sh=null; System.out.println("1.Circle\n2.Square\n3.Reactangle"); System.out.println("Enter shape no");

int ch=sc.nextInt(); switch(ch){


case 1: System.out.println("Enter Radius :"); double r=sc.nextDouble();

sh=new Circle(r); break;

case 2: System.out.println("Enter side :"); double side=sc.nextDouble();

sh=new Square(side); break;

case 3: System.out.println("Enter side :"); double l=sc.nextDouble();

double b=sc.nextDouble(); sh=new Reactangle(l,b); break;

default:

 

System.out.println("No shape matched"); sh=null;

break;

}

 

 

if(sh!=null)

shapes.add(sh);

System.out.println("do u have more shapes");


String s=sc.next(); if(s.equalsIgnoreCase("no")){

break;

}

 

}

 

return shapes;

 

}

private static Shape BiggestAreashape(List<Shape> ls){ Shape b=ls.get(0);

Iterator<Shape> it=ls.iterator();

 

 

while(it.hasNext()){

 

Shape s=it.next(); if(b.getArea()<s.getArea())

b=s;

}

return b;

 

 

}

 

 

static void displayshape(List<Shape> ls){


for(Shape shape:ls){

System.out.println(shape);

}

 

 

 

}

 

 

 

 

public static void main(String[] args) { List<Shape> ls =getShapes(); Shape big=BiggestAreashape(ls);

System.out.println("Shapes info :"); displayshape(ls); System.out.println("biggest Area shape is:"); System.out.println(big);

 

 

}

 

}

 

 

o/p:-  1.Circle 2.Square 3.Reactangle


Enter shape no 1

Enter Radius :

5

do u have more shapes yes

1.Circle 2.Square 3.Reactangle Enter shape no 2

Enter side :

6

do u have more shapes no

Shapes info :

Circle [radius=5.0 Area= 78.5] Square [ side = 6.0 Area = 36.0] biggest Area shape is:

Circle [radius=5.0 Area= 78.5]


      String Programs

 

 

Write a program to find weather a string is ANAGRAM or not?

 

Def: a word, phrase, or name formed by rearranging the letters of another, such as silent

 

formed from listen

import java.util.Scanner;

 

 

public class Anagram {

static boolean isanagram(String st1,String st2){ int count1[]=noofOccurence(st1);

int count2[]=noofOccurence(st2); for (int i = 0; i < count1.length; i++) {

if(count1[i]!=count2[i]) return false;

 

 

}

return true;

}


static int[] noofOccurence(String st){ int[]count=new int[26];

 

for (int i = 0; i < st.length(); i++) { char ch=st.charAt(i); if(ch>='A'&&ch<='Z'){

count[ch-65]++;

}

if(ch>='a'&&ch<='z'){

count[ch-97]++;

}

 

 

}

return count;

 

}

public static void main(String[] args) { Anagram an=new Anagram(); Scanner sc=new Scanner(System.in);

System.out.println("Enter first String"); String st1=sc.nextLine(); System.out.println("Enter second String"); String st2=sc.nextLine();


boolean bb=an.isanagram(st2, st1); if(bb)

System.out.println(st1+" and "+st2+" are anagram");

else

System.out.println("not ");

 

 

 

}

 

 

 

}

 

 

 

 

o/p:-

 

 

Enter first String reverse

Enter second String serever

reverse and serever are anagram

 

 

============================================================


Write  program  weather  the  string  is  PANAGRAM  or not?

 

Def: a sentence containing every letter of the alphabet

 

import java.util.Scanner;

 

 

public class Panagram {

 

static boolean ispanagram(String st){ int[]count=new int[26];

 

 

for (int i = 0; i < st.length(); i++) { char ch=st.charAt(i); if(ch>='A'&&ch<='Z'){

count[ch-65]++;

}

if(ch>='a'&&ch<='z'){

 

count[ch-97]++;

}

 

 

}

for(int i=0;i<count.length;i++){ System.out.println((char)(i+65)+"--> "+count[i]);


if(count[i]==0)

return false;

 

}

return true;

 

}

 

public static void main(String args[])

{

Scanner sc=new Scanner(System.in); System.out.println("Enter the sentence."); String s=sc.nextLine(); System.out.println(ispanagram(s));

 

 

}

 

}

 

 

Write a program check the given string is PALINDROME or not?

 

import java.util.Scanner;

 

 

public class Palindrom {

static boolean ispalindrom(String st){


int i=0; while(i<st.length()/2){

if(st.charAt(i)!=st.charAt(st.length()-1-i)) return false;

i++;

 

}

return true;

 

}

public static void main(String args[])

{

Scanner sc=new Scanner(System.in); System.out.println("Enter the sentence."); String s=sc.nextLine();

boolean str=ispalindrom(s); if(str)

System.out.println(s+" is palindrom");

else

System.out.println(s+ " not palindrom");

 

 

}

 

}


o.p:-

Enter the sentence. qwerty ytrewq

qwerty ytrewq is palindrom

 

 

 

 

 

Write     a     program     to     display     REVERSE     of     a STRING/SENTENCE?

 

import java.util.Scanner;

 

 

public class ReverseString {

public static void main(String args[])

{

Scanner sc=new Scanner(System.in); System.out.println("Enter the sentence."); String s=sc.nextLine(); System.out.println("Aftr conversion"); String str=reverseSentence(s); System.out.println(str);


}

 

 

public static String reverseSentence(String st) { char ch[]=st.toCharArray();

st=" ";

 

for (int i = ch.length-1; i >=0; i--){ int k=i; while(i>=0&&ch[i]!=' '){

i--;

}

int j=i+1; while(j<=k){

st=st+ch[j]; j++;

}

if(i>0)

st=st+ch[i];

}

return st;

}

 

}


o/p:

Enter the sentence. qwerty id

Aftr conversion id qwerty

 

 

Write a program to display REVERSE of a Word?

 

import java.util.Scanner;

 

 

public class ReverseWord {

public static void main(String args[])

{

 

Scanner sc=new Scanner(System.in); System.out.println("Enter the sentence."); String s=sc.nextLine(); System.out.println("Aftr conversion"); String str=reversewordSentence(s); System.out.println(str);

 

 

}

public static String reversewordSentence(String st) {


char ch[]=st.toCharArray(); st=" ";

for (int i =0; i <=ch.length; i++){ int k=i;

while(i<ch.length&&ch[i]!=' '){ i++;

}

int j=i-1; while(j>=k){

st=st+ch[j]; j--;

}

 

if(i<ch.length)

st=st+ch[i];

 

}

return st;

 

}

 

}

 

 

o/p:-

Enter the sentence. qwertty


Aftr conversion yttrewq

 

Write a program to COUNT number of CHARACTERS in a String?

 

import java.util.Scanner;

 

 

public class CountCharacter {

static int countchar(String st){ int count=0;

char ch[]=st.toCharArray();

for (int i = 0; i < ch.length; i++) { if(ch[i]>=65&&ch[i]<=90 ||ch[i]>=97 &&

ch[i]<=122||ch[i]>=48&&ch[i]<=57 && ch[i]!=32 && ch[i]!=',' &&ch[i]!='.')

count++;

 

 

}

return count;

}

public static void main(String[] args) { Scanner sc=new Scanner(System.in);


System.out.println("Enter the word"); String st=sc.nextLine();

int chr=countchar(st); System.out.println("total no charcters = "+chr);

 

 

}

 

}

 

 

o/p:-

 

 

Enter the word qwerty

total no charcters = 6

 

 

Write a program to COUNT number of WORDS in a String?

 

 

import java.util.Scanner;

 

 

public class CountWord {

static int countWords(String st){ int wc=0;

char ch[]=st.toCharArray();


for (int i = 0; i < ch.length; i++) {

if(i==0&&ch[i]!=' '||ch[i]!=' '&&ch[i-1]==' ') wc++;

 

 

}

 

return wc;

 

}

public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter the word"); String st=sc.nextLine();

int chr=countWords(st); System.out.println("total no word = "+chr);

 

}

 

}

 

 

O/P:-Enter the word QWERTY ID IS NON

total no word = 4


Write  a  program  to  find  the sum of numbers in an ALPHA NUMERIC STRING?

 

import java.util.Scanner;

 

 

public class Sumofdigit {

static int sumOfDigit(String st){ int sum=0;

for (int i = 0; i < st.length(); i++) { char ch=st.charAt(i); if(ch>=48 &&ch<=57){

sum=sum+(ch-48);

}

}

 

return sum;

}

 

 

public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter the word"); String st=sc.nextLine();

int sm=sumOfDigit(st);


System.out.println("Total sum is "+sm);

 

 

 

}

 

 

 

}

 

 

O/P:-

Enter the word Q1W2E3R45T

Total sum is 15

 

 

Write a program to display number of LOWERCASE, UPPERCASE, SPECIAL SYMBOLS, SPACES and DIGITS in a STRING?

 

import java.util.Scanner;

 

 

public class CountingString {

 

public static void main(String[] args) { Scanner scn=new Scanner(System.in);

System.out.println("Enter the Sentence"); String st=scn.nextLine();


//ac=alphabets;

//vc=vowels;

//cc=consonents

//dc=degits

//uc=upper case

 

//lc=lower case

//sc=special character

int ac=0,vc=0,cc=0,dc=0,uc=0,lc=0,sc=0; for(int i=0;i<st.length();i++){

char ch=st.charAt(i); if(ch>=65&&ch<=90){

ac++; uc++;

if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')

 

 

vc++;

else

cc++;

}

else if(ch>='a'&&ch<='z'){ ac++;

lc++;


if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u') vc++;

else

cc++;

 

}

 

else if(ch>=48&&ch<=57) dc++;

 

else

sc++;

 

}

System.out.println("no of alphabets ="+ac); System.out.println("no of degits ="+dc); System.out.println("Total upper case="+uc); System.out.println("total lower case="+lc); System.out.println("total vowels ="+vc); System.out.println("total consonents"+cc); System.out.println("total special characters = "+sc);

}

 

 

 

}


O/P:-

Enter the Sentence QWERTT Y KM

no of alphabets = 9 no of degits = 0 Total upper case= 9 total lower case = 0 total vowels = 1 total consonents = 8

total special characters = 2

 

 

 

 

Write a program to convert NUMBER into WORDS?

 

import java.util.Scanner;

 

 

import twodimensional.MainRunner;

 

 

public class NointoWords {

 

 

String one[]={" ","one","two","three","four","five","Six","Seven","Eight","Nine","Ten","Eleven","T


welve","thirteen","fourteen","fifteen","Sixteen","Seventeen","Eightteen","Ninete en"};

String two[]={" "," ","Twenty","thirty","fouty","fifty","Sixty","Sevety","Eighty","ninty"};

 

 

 

 

void ntow(int n,String st){ if(n<20)

System.out.print(one[n]);

 

 

else

 

System.out.print(two[n/10]+one[n%10]);

 

 

if(n!=0)

System.out.print(st+" ");

 

 

}

public static void main(String[] args) { NointoWords nw=new NointoWords();

 

 

Scanner sc=new Scanner(System.in); System.out.println("Enter the no");


int n=sc.nextInt();

 

 

nw.ntow(n/10000000,"crore"); nw.ntow(n/100000%100,"lakh"); nw.ntow(n/1000%100,"Thousand"); nw.ntow(n/100%10,"hundred"); nw.ntow(n%100," ");

}

 

}

 

 

O/P:-

Enter the no 145

onehundred foutyfive

 

 

Write a program to display STRING INITCAP of Words?

 

 

import java.util.Scanner;

 

 

public class ConvertFirstCapital {


static String initcap(String st){ char ch[]=st.toCharArray();

for (int i = 0; i < ch.length; i++) {

if(i==0&&ch[i]!=' '||ch[i]!=' '&&ch[i-1]==' '){

 

 

if(ch[i]>='a'&&ch[i]<='z'){

ch[i]=(char)(ch[i]-32);

}

}

else{

if(ch[i]>='A'&&ch[i]<='Z'){

ch[i]=(char)(ch[i]+32);

 

}

}

 

}

st=new String(ch); return st;

}

public static void main(String args[])

{

Scanner sc=new Scanner(System.in); System.out.println("Enter the sentence.");


String s=sc.nextLine();

 

 

System.out.println("Aftr conversion"); String str=initcap(s); System.out.println(str);

 

 

}

 

 

 

}

 

 

O/P:-

 

 

Enter the sentence.

RANMDND

 

Aftr conversion Ranmdnd

 

Write a program to convert UPPER CASE TO LOWER CASE?

 

import java.util.Scanner;


public class ConvertLowercase {

 

 

 

 

 

 

 

String toLowerCase(String str)

 

{

char ch[]=str.toCharArray();

for (int i = 0; i < ch.length; i++) { if(ch[i]>=65&&ch[i]<=90){

ch[i]=(char) (ch[i]+32);

}

 

 

}

 

String st=new String(ch);//Character array to String return st;

 

 

}

 

 

String tolowercase(String str){ String ls="";

for (int i = 0; i < str.length(); i++) {


char ch=str.charAt(i); if(ch>=65&&ch<=90){

ch=(char) (ch+32);

}

ls=ls+ch;

 

}

return ls;

 

}

 

 

public static void main(String[] args) { ConvertLowercase lc=new ConvertLowercase(); Scanner sc=new Scanner(System.in); System.out.println("Enter the Word");

String sd=sc.nextLine(); String s=lc.tolowercase(sd) ;

System.out.println("Enter the Word"); String sd1=sc.nextLine();

String st=lc.toLowerCase(sd1) ; System.out.println(s); System.out.println(st);

}


}

 

 

O/P:-

Enter the Word QWERT

Enter the Word REWQ

qwert rewq

 

Write a program to convert LOWER CASE TO UPPER CASE ?

 

 

import java.util.Scanner;

 

 

public class ConvertUppercase {

 

 

String touppercase(String str){ String ls="";

for (int i = 0; i < str.length(); i++) { char ch=str.charAt(i);


if(ch>=97&&ch<=122){ ch=(char) (ch-32);

}

ls=ls+ch;

 

}

 

return ls;

 

}

 

 

public static void main(String[] args) { ConvertUppercase uc=new ConvertUppercase(); Scanner sc=new Scanner(System.in); System.out.println("Enter the word");

String st=sc.nextLine(); String s=uc.touppercase(st) ;

System.out.println("the uppercase of "+st+" is "+s);

 

 

}

 

}

 

 

O/P:-

Enter the word qwerty


the uppercase of qwerty is QWERTY

 

 

 

 

write a program to search the word

 

import java.util.Scanner; public class WordSearch

{

public static void main(String[] args)

{

Scanner sc= new Scanner(System.in); System.out.println("Enter the sentence "); String st=sc.nextLine();

System.out.println("Enter the word to search: "); String w=sc.next();

boolean rs= checkWord(st,w); if(rs)

System.out.println("yes word is there ");

 

else

System.out.println("no word is not there ");

 

}


 

 

private static boolean subStr(String st, String w)

{

char c1[]=st.toCharArray(); char c2[]=w.toCharArray();

 

 

for(int i=0;i<c1.length;i++)

{

int j=0; int k=i;

while(k<c1.length&&j<c2.length&&c1[k]==c2[j])

{

 

k++; j++;

}

if(j==c2.length)

return true;

}

 

return false;

}

private static int countsubStr(String st, String w)

{


char c1[]=st.toCharArray(); char c2[]=w.toCharArray(); int count=0;

for(int i=0;i<c1.length;i++)

{

 

int j=0; int k=i;

while(k<c1.length&&j<c2.length&&c1[k]==c2[j])

{

k++; j++;

}

if(j==c2.length)

count++;

 

}

return count;

 

}

 

 

private static int indexsubStr(String st, String w)

{

char c1[]=st.toCharArray(); char c2[]=w.toCharArray();


int count=0;

for(int i=0;i<c1.length;i++)

{

int j=0; int k=i;

while(k<c1.length&&j<c2.length&&c1[k]==c2[j])

{

k++; j++;

}

if(j==c2.length)

return i;

}

return -1;

 

}

 

 

private static int indexsubStr(String st, String w,int oc)

{

 

char c1[]=st.toCharArray(); char c2[]=w.toCharArray(); int count=0;

for(int i=0;i<c1.length;i++)


{

 

int j=0; int k=i;

while(k<c1.length&&j<c2.length&&c1[k]==c2[j])

{

 

k++; j++;

}

if(j==c2.length)

{

count++; if(count==oc)

return i;

}

 

 

 

}

return -1;

 

}


private static boolean checkWord(String st, String w)

{

char c1[]=st.toCharArray(); char c2[]=w.toCharArray();

 

 

for(int i=0;i<c1.length;i++)

 

{

int j=0; int k=i;

while(k<c1.length&&j<c2.length&&c1[k]==c2[j])

{

k++; j++;

}

 

if(j==c2.length&&(i==0||c1[i-1]==' ')&&(k==c1.length||c1[k]==' ')) return true;

}

return false;

}

 

 

}


o/p:-

Enter the sentence qwerty u tyu

Enter the word to search:

tyu

 

yes word is there

 

 

Write a program to count nodays ,years ,month bw given dates

 

import java.nio.charset.MalformedInputException;

 

import java.util.Scanner;

 

 

public class Date {

int dd,mm,yy;

 

int month[]={0,31,28,31,30,31,30,31,31,30,31,30,31};

public Date(int dd, int mm, int yy) {

 

 

this.dd = dd; this.mm = mm; this.yy = yy;

if(yy%4==0&&yy%100!=0||yy%400==0)


month[2]=29;

 

}

 

 

int countnodays(){

int y=yy-1; int days=0; days=y*365;

days=days+(y/4-y/100+y/400); for(int i=1;i<mm;i++){

days=days+month[i];

}

days=days+dd; return days;

 

 

 

}

static Date readdate(){

Scanner sc=new Scanner(System.in); System.out.println("Enter dd mm yyyy"); int dd=sc.nextInt();

int mm=sc.nextInt(); int yy=sc.nextInt();


Date d=new Date(dd,mm,yy); return d;

 

 

}

 

 

@Override

public String toString() {

return "Date [" + dd + "/" + mm + "/" + yy + "]";

}

public static void main(String[] args) { System.out.println("enter first date :"); Date d1=Date.readdate(); System.out.println("Enter 2nd date :"); Date d2=Date.readdate();

 

 

int days=d2.countnodays()-d1.countnodays();

 

 

 

System.out.println("no of days between"+d1+" and "+d2+" is

"+days);

 

System.out.println("total year,months till dates "+days/365+" "+days%365/30);

System.out.println("no of years between"+d1+" and "+d2+" is "+days/365);


}

 

}

 

 

output

 

 

enter first date :

Enter dd mm yyyy 03 02 1997

Enter 2nd date :

Enter dd mm yyyy 02 03 1998

no of days betweenDate [3/2/1997] and Date [2/3/1998] is 392 total year,months till dates 1 0

no of years betweenDate [3/2/1997] and Date [2/3/1998] is 1