How to check if a number ends with 2? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to check if a number ends with 2?

30th Nov 2016, 3:04 PM
RAJDEEP DAS
RAJDEEP DAS - avatar
5 Answers
+ 4
if(x%10==2) ends with 2 else doesnt end with 2
30th Nov 2016, 3:10 PM
Ethan
Ethan - avatar
+ 1
you can use Strings .. so you input the number as string and then cheack if the last number is equal to 2 this code will be thing like this Scanner in =new Scanner (System.in); String n=in.next(); if(n.charAt(n.length()-1)=='2') System.out.println("yes"); else System.out.println("no");
30th Nov 2016, 5:21 PM
abdulmalek dery
abdulmalek dery - avatar
+ 1
welcome
30th Nov 2016, 9:20 PM
abdulmalek dery
abdulmalek dery - avatar
0
thanks a lot👍
30th Nov 2016, 3:11 PM
RAJDEEP DAS
RAJDEEP DAS - avatar
0
thanks again
30th Nov 2016, 5:25 PM
RAJDEEP DAS
RAJDEEP DAS - avatar