Friday, 2 December 2016

Write a program to check wether the given number is anagram or not?

Lot of people facing programming problem in every Software Job interview. Now a days, one programming question is always asking by interviwer is "Write a program to check wether the given number is anagram or not?". So today we are here to solve to problem in this blog. Let me first take the defination of Anagram Number that is taken from google and this is the best defination that anyone can easily understand.

An anagram is direct word switch or word play, the result of rearranging the letters of a word or phrase to produce a new word or phrase, using all the original letters exactly once; for example, the word anagram can be rearranged into nag-a-ram. Someone who creates anagrams may be called an "anagrammatist".





Write a program to check wethere the given number is anagram or not?

import java.util.*;
class AnagramNumber
{
 public static void main(String[] args)
 {
  int n1=12345;
  int n2=43251;

  String s1=String.valueOf(n1);
  String s2=String.valueOf(n2);
  //System.out.println(s1+s2);

  char[] ch1=s1.toCharArray();
  //System.out.println(ch1);
  
  char[] ch2=s2.toCharArray();
  //System.out.println(ch2);

  Arrays.sort(ch1);
  Arrays.sort(ch2);
  //System.out.println(ch2);
  boolean flag=false;
  for(int i=0;i<ch1.length;i++){
    if(ch1[i]==ch2[i]){
     flag=true;

    }else{
     flag=false;
    }

  }
  if(flag==true){
   System.out.println("anagram number");
  }else{
   System.out.println("not anagram number");
  }
 }
}

Keep visiting for more programs....Happy Coding....

Thank you(SmartiesGeeks)

1 comment:

Make Top Rank Blog