Thursday, January 30, 2014

Java program for palindrome - For String

Write a program to find Whether given string is palindrome or not.


import java.util.Scanner;

public class Stringpolindrome {
    public static void main(String[] args) {
  
        int i,j=0;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter your String:-");
        String str=sc.next();
        char c[]=new char[20];
        c=str.toCharArray();
        i=str.length();
        --i;

    while(j<=i)
    {
        if(c[i]!=c[j])
        {
            System.out.println("The given string "+ str +" is not polindrome");
            System.exit(0); 
        }
        ++j;
        --i;
      
    }
    System.out.println("The given string" + str +"is polindrome");
    }
}
Output 1: 
Enter your String:- javainchennai 
The given string javainchennai is not polindrome 

OUTPUT-2 :
Enter your String:- malayalam 

The given string malayalam is palindrome





To write a Reverse of a String

Write a program for a Reverse of a String


import java.util.Scanner;



public class StringReverse {

    public static void main(String[] args) {



        Scanner sc=new Scanner(System.in);

        System.out.println("Enter a String");

        String str=sc.nextLine();

        int strLen=str.length();

        char c[]=new char[20];

       

        for(int i=0,j=strLen-1;i<strLen;i++,j--)

        {

            c[i]=str.charAt(j);

            System.out.print(c[i]);

        }   

    }

}



OUTPUT:-

Enter a String

vinodh bandu

udnab hdoniv





To Sort a Number in Ascending Order

Write a program to sort a number in ascending order


import java.util.Scanner;

public class SortNumbersInAscendingOrder

{

    public static void main(String[] args)

{

	Scanner sc=new Scanner(System.in);

	System.out.println("Enter Number of elements:-");

		int limit=sc.nextInt();

		System.out.println(" Enter Elements:- ");

		int a[]=new int[10];

		for(int  i=1;i<=limit;i++)

		{  

		   a[i]=sc.nextInt();

		}

		 for(int  i=0;i<limit-1;i++)

            for(int j=i;j<limit;j++)

				if(a[i]>a[j])

				{

				int temp=a[i];

				a[i]=a[j];

				a[j]=temp;

				}

		  System.out.println("The sorted list is:-");

		  for( int i=0;i<limit;i++)

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

        }

}


OUTPUT:

Enter Number of elements:-

5

Enter Elements:-

15 25 16 12 4

The sorted list is:- 

0   12   15   16   25




To Sort the Digits of a Number in Ascending Order

Write a program to Sort the Digits of a Number in Ascending Order


import java.util.Scanner;
public class SortDigitsInInteger

{

    public static void main(String[] args)

	{
		Scanner sc=new Scanner(System.in);

		System.out.println("Enter a number:-");

		int num=sc.nextInt();

		int a,i=0;

		int sort[]=new int[10];

		   while(num>0)

      {

	   a=num%10;

	   sort[i]=a;

	   i++;

	   num=num/10;

     }

	   for(int j=0;j<i-1;j++)

	  for(int k=j+1;k<i;k++)

		   if(sort[j]>sort[k])

		  {

		   int temp=sort[j];

		   sort[j]=sort[k];

		   sort[k]=temp;

		  }

		 System.out.println("The digits in number ascending order:");

		 for(int j=0;j<i;j++)

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

     }

}



OUTPUT:

Enter a number:-

4261

The digits in number ascending order:

1 2 4 6





To Display Multiplication Table

Write a program to display multiplication table


import java.util.Scanner;



public class multiplicationtable

{

          public static void main(String[] args)

{

            System.out.println("Enter the table number and the count value:");

            Scanner sc=new Scanner(System.in);

            int tnum=sc.nextInt();

            int count=sc.nextInt();

            System.out.println("The table is ...");
            for(int i=1;i<=count;i++){

            System.out.println(tnum+"*"+i+"=" + i*tnum);

                        

                        }

            }

}

  

OUTPUT:



Enter the table number and the count value:

8

10



The table is ...

8*1=8

8*2=16

8*3=24

8*4=32

8*5=40

8*6=48

8*7=56

8*8=64

8*9=72

8*10=80




To find given number is Prime or Not

Write a program to find given number is Prime or Not


import java.util.Scanner;
public class PrimeOrNot

{

    public static void main(String[] args)

  {
		 System.out.println("Enter the number:");
		 Scanner sc=new Scanner(System.in);
		 int i;
		 int flag=0;
		 long num=sc.nextLong();

		 if(num<=3)

			 System.out.println("The given number is prime:");

		 else
		 
		 for(i=2;i<=num/2;i++)

			  if(num%i==0)

		  {
		  flag=1;
		  break;

		  }

		else

		  flag=0;

		  if(flag==1)
				System.out.println("The given number "+num+"  is not prime");
		  else
				System.out.println("The given number "+num+" is prime");
			}
}



OUTPUT 1:


Enter the number:
5

The given number 5 is prime



OUTPUT 2:

Enter the number:
55


The given number 55  is not prime





To Find given Number is Palindrome or Not

Write a program to find whether given Number is Palindrome or Not


import java.util.Scanner;

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

 {

		 int mod=0;

		 int c=0;

		 int d=0;

		 int num1=0;

		 System.out.println("Enter a number:");

		 Scanner sc=new Scanner(System.in);

		 int num=sc.nextInt();

		 num1=num;



   while(num>0)

   {

	   mod=num%10;

	   c=c+mod;

	   num=num/10;

	   d=mod+d*10;

   }

   if(num1==d)
   {   
		System.out.println("The given number "+num1+" is palindrome");
   }

   else

   {
	   System.out.println("The given number "+num1+" is not palindrome");
   }
 }

}



OUTPUT:

Enter a number:
1523


The given number 1523 is not palindrome