Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
3.00/5 (4 votes)
See more:
Java
if (x % 100 == 11 || x % 100 == 22 || x % 100 == 33 || x % 100 == 44
				|| x % 100 == 55 || x % 100 == 66 || x % 100 == 77
				|| x % 100 == 88 || x % 100 == 99 || x % 100 == 00) {

			return true;
		}
Posted
Updated 18-Sep-14 4:23am
v2

(I don't know Java.)

How about:

Java
return (x % 100 == 11 || x % 100 == 22 || x % 100 == 33 || x % 100 == 44
  || x % 100 == 55 || x % 100 == 66 || x % 100 == 77
  || x % 100 == 88 || x % 100 == 99 || x % 100 == 00) ;


You could use a local variable to hold x % 100 and save some typing.
I suppose you could use a switch or some sort of lookup table.

Also, consider this:

C#
int x = 455 ;
int x1 = x % 10 ;
int x10 = x / 10 % 10 ;

bool b = x1 == x10 ;


So, to put that all together:

Java
public boolean doSomeCheck(int x){ return ( x % 10 ) == ( x / 10 % 10 ) ; }
 
Share this answer
 
v3
Comments
hypermellow 18-Sep-14 11:04am    
The 2nd of your approachs is really nice! 5'd
shashanknautiyal 18-Sep-14 11:05am    
thank you for your suggestion ...still working on it
Sergey Alexandrovich Kryukov 18-Sep-14 13:30pm    
Step by step... 5ed.

Even the first sample is useful: it helps to demonstrate how silly are the lines like
if (A)
return true; // but A is already Boolean!
// and what to return is not A? :-)


—SA
C#
int tempINT = x % 100;
            if (tempINT % 11 == 0
                || tempINT == 0)
            {
                return true;
            }
 
Share this answer
 
Comments
hypermellow 18-Sep-14 11:03am    
Nice! 5'd
shashanknautiyal 18-Sep-14 11:04am    
thank you for your suggestion ..i want to check whether a number has the same last two digits or not and returns true or false in simple way with out switch case if possible
TorstenH. 19-Sep-14 2:11am    
+5!

@OP: You need to figure a way to cut the number down to a 2 digit.
Further to other answers, you want to know that the last two digits are divisible by 11:

Java
boolean lastTwoEleven(int x)
{
    // mod 100 gives last two digits & mod 11 checks if divisble
    return ((x % 100) % 11) == 0;
}
 
Share this answer
 
Hi,

You could use a switch statement:

[update}
Since you are looking to reuse this logic, it would be better to wrap it in a function, that way you can call it multiple times (without rewriting it).

Java
if(doSomeCheck(x)==true){
 ... do stuff
}else{
 ... do other stuff
}

if(doSomeCheck(y)==true){
 ... do stuff
}else{
 ... do other stuff
}


public boolean doSomeCheck(int x){
 boolean bOut=false;
 switch(x % 100){
  case 0:
  case 11:
  case 22:
  case 33:
  case 44:
  case 55:
  case 66:
  case 77:
  case 88:
  case 99:
   bOut=true;
   break;
  default:
   bOut=false;
 }
 return bOut;
}


Hope it helps.
 
Share this answer
 
v4
Comments
shashanknautiyal 18-Sep-14 10:52am    
thank u for your suggestion i have four int variables x,y,z,w for all four i have to compare it with this values (11,22,33.....) so i have to build four switch case ?
actually i want to find which int variable has last two no same(like 45611 ) and output would be in boolean
hypermellow 18-Sep-14 11:00am    
Hi, I've updated my solution to avoid re-writing the switch statement multiple times.

Hope it helps.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900