Friday, October 31, 2014

Java Important Pro



Java program to find whether no. is palindrome or not

class Palindrome
{
   public static void main(String args[])
   {
               int num = Integer.parseInt(args[0]);
               int n = num;
               int reverse=0,remainder;
               while(num > 0)
                {
                           remainder = num % 10;
                           reverse = reverse * 10 + remainder;
                           num = num / 10;
               }
               if(reverse == n)
                           System.out.println(n+" is a Palindrome Number");
               else
                           System.out.println(n+" is not a Palindrome Number");
   }
}

Java palindrome number

public class JavaPalindromeNumberExample
{
public static void main(String[] args)
{
int numbers[] = new int[]{121,13,34,11,22,54};
for(int i=0; i < numbers.length; i++)
{                        
int number = numbers[i];
int reversedNumber  = 0;
int temp=0;
while(number > 0)
{
temp = number % 10;
number = number / 10;
reversedNumber = reversedNumber * 10 + temp;
if(numbers[i] == reversedNumber)
System.out.println(numbers[i] + " is a palindrome number");
else
System.out.println(numbers[i] + " is not a palindrome number");
          }
    }
}
 
Write a java program to check number string is palindrome or not with output
 
            import java.util.*;
            public class Palindrome 
{
    public static void main(String[] args) 
    {
        Scanner scanner = new Scanner(System.in);
                     System.out.println("Please enter a number: ");
        int num = scanner.nextInt();
        System.out.println("Please enter a string: ");
        String str = scanner.next();
                     Palindrome palin = new Palindrome();
        int revNum = palin.reverse(num);
        String revStr = palin.reverse(str);
                     if (num == revNum) 
       {
            System.out.printf("\n The number %d is a Palindrome ", num);
        } 
        else 
        {
            System.out.printf("\n The number %d is not a Palindrome ", num);
        }
        if (str.equalsIgnoreCase(revStr)) 
       {
            System.out.printf("\n The string '%s' is a Palindrome ", str);
        } 
        else 
          {
            System.out.printf("\n The string '%s' is not a Palindrome ", str);
        }
    }
    public int reverse(int num) 
   {
        int revNum = 0;
        while (num > 0) 
         {
            int rem = num % 10;
            revNum = (revNum * 10) + rem;
            num = num / 10;
        }
        return revNum;
    }
     public String reverse(String str) 
     {
        StringBuilder revStr = new StringBuilder();
        for (int i = str.length()-1; i >= 0; i--) 
       {
            revStr.append(str.charAt(i));
        }
        return revStr.toString();
    }
}
 
Find reverse of a given number and string with output

import java.util.*;
          public class RevNumString 
{
    public static void main(String[] args) 
    {
        Scanner scanner = new Scanner(System.in);
                     System.out.println("Please enter a number: ");
        int num = scanner.nextInt();
        System.out.println("Please enter a string: ");
        String str = scanner.next();
                     RevNumString rns = new RevNumString();
        int revNum = rns.reverse(num);
        String revStr = rns.reverse(str);
        
        System.out.printf("\n The reverse of number %d is %d ", num, revNum);
        System.out.printf("\n The reverse of string '%s' is '%s' ", str, revStr);
    }
    public int reverse(int num) 
    {
        int revNum = 0;
        while (num > 0) 
        {
            int rem = num % 10;
            revNum = (revNum * 10) + rem;
            num = num / 10;
        }
        return revNum;
    }
    public String reverse(String str) 
    {
        StringBuilder revStr = new StringBuilder();
        for (int i = str.length()-1; i >= 0; i--) 
        {
            revStr.append(str.charAt(i));
        }
        return revStr.toString();
    }
}

No comments:

Post a Comment