Java code to check weather the second number is rotated version of the first number without converting it to string or array....frequently asked in interviews.....
//*********************************Coded-By : SOMES KUMAR K.*******************************//
//*********************************LEARN CODE WITH FUN*************************************//
import java.util.Scanner; public class Find_Number_Rotated { int find_count(int a){ int c=0; while(a>0){ c++; a=a/10; } return c; } public static void main(String[] args) { Find_Number_Rotated no = new Find_Number_Rotated(); boolean xc=false; Scanner s1 = new Scanner(System.in); int a= s1.nextInt(); int data = s1.nextInt(); int c = no.find_count(a); int th=1; for(int i=0;i<c;i++) th*=10; int b=(a*th)+a; int s7=(th*th)/10; while(th>1){ int x =b/th; if(x-data==0){ xc=true; System.out.println(true); break; } b=b%s7; s7=s7/10; th=th/10; } if(!xc) System.out.println(xc); } }
OUTPUT:
123 321 false
123 231 true
9876 8769 true
9876 6789 false
//****************************Any doubts??____drop a comment****************//
Comments
Post a Comment