Showing posts with label reverse. Show all posts
Showing posts with label reverse. Show all posts

Friday, 17 June 2016

Program to reverse a string without using String API.

Explanation :
If str ="Hello world" then output will be "dlrow olleH".

Code:

public class Reverse
{
       public static void main(String[] args)
      {
           String str="Hello world";
           String rev="";
          for(int i=str.length()-1;i>=0;--i)
          {
            revs +=str.charAt(i);
           }

           System.out.println(rev);
}
}

Program to know whether a number is Magic Number or not.

Explanation :
If n=1729 then find the sum of digits of the given number i.e,
(1 + 7 + 2 + 9 =19). Reverse of 19 is 91.
Then (19 X 91 = 1729).
If the obtained product value and the given input are same, then the given number is a magic number.

Code:

import java.util.Scanner;
public class MagicNumber
{
    public static void main (String args[])
    {
        int num, sum, rev;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the value for num:");
        num=sc.nextInt();
        MagicNumber m=new MagicNumber();
        sum = m.sumOfDigits(num);
        rev = m.reverse(sum);
        if (sum < 10)
        {
                if ((sum * sum) == num)
                {
                    System.out.println(num+" is a magic number");
                }
                else
                {
                    System.out.println(num+" is not a magic number");
                }
              
        }
        else if ((sum * rev) == num)
        {
            System.out.println(num+" is a magic number");
        }
        else
        {
            System.out.println(num+" is not a magic number");
        }
        sc.close();
    }
   
    public int sumOfDigits(int n)
    {
        int s = 0;
        while (n > 0)
        {
                s = s + (n % 10);
                n = n / 10;
        }
        return s;
    }

    public int reverse(int num)
    {
        int rev = 0;
        while (num > 0)
        {
                rev = (rev * 10) + (num % 10);
                num = num / 10;
        }
        return rev;
    }
}

Thursday, 16 June 2016

Program to find the reverse of a number.

Code:

import java.util.Scanner; 
class ReverseNumber 
    public static void main(String args[]) 
    {
        int n, reverse = 0;
       System.out.println("Enter the number to reverse"); 
       Scanner in = new Scanner(System.in); 
        n = in.nextInt(); 
       while( n != 0 ) 
      { 
              reverse = reverse * 10; 
              reverse = reverse + n%10; 
              n = n/10; 
       } 
        System.out.println("Reverse of entered number is "+reverse); 
     }
 }

Program to know whether a number is Palindrome or not.

Explanation :
Palindrome means "a number which equal to it's reverse".
(If n=151 then it is a Palindrome as it's reverse is also 151).

Code:

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 ");
            else System.out.println(n+" is not a Palindrome");
       }
}

Output:  java Palindrome 1331
                1331 is a Palindrome


Program to know whether a number or a String is Palindrome or not using Recursion.

Explanation :
Palindrome means "a number which equal to it's reverse".
(If n=151 then it is a Palindrome as it's reverse is also 151).
Recursion means calling a method within itself until it returns a value.

Code:

import java.util.Scanner;
class PalindromeCheck
{
    public static void main(String[]args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the String for check:");
        String string = sc.nextLine();
        if(isPal(string))
            System.out.println(string + " is a palindrome");
        else
            System.out.println(string + " is not a palindrome");
    }
   
    public static boolean isPal(String s)
    {  
        if(s.length() == 0 || s.length() == 1)
              return true;
        if(s.charAt(0) == s.charAt(s.length()-1))
              return isPal(s.substring(1, s.length()-1));
        return false;//This statement will be not executed if String is palindrome
    }
}

Wednesday, 15 June 2016

Program to reverse an Integer using StringBuffer class.

If n1=16, n2=26 then output must be 321.
Explanation: 16,26 should be reversed as 61,62 and added.
Then addition result should be reversed and printed.    

Code:

public class Stringbuffer
{
   public static void main(String[] args)
  {
        StringBuffer sb=new StringBuffer("16");
        StringBuffer sb1=new StringBuffer("26");
        StringBuffer sb2;
       
        sb.reverse();
        sb1.reverse();
       
        String s=sb.toString();
        int n1=Integer.parseInt(s);
       
        String s1=sb1.toString();
        int n2=Integer.parseInt(s1);
       
        int n3=n1+n2;
               
        sb2=new StringBuffer();
        sb2.append(n3);
        sb2.reverse();
        String s3=sb2.toString();
        int n4=Integer.parseInt(s3);
        System.out.println(n4);
    }
}