Skip to main content

Posts

Showing posts from January, 2018

Palindrome

package ae.naveed; import java.util.Scanner; public class FindPalindrome { public static void main(String[] args) { Scanner scanner = new Scanner(System. in ); char [] inputString = scanner.nextLine().toCharArray(); int i = 0 , j = inputString. length - 1 ; while (j > i ) { if (inputString[i] != inputString[j]) { System. out .println( "NO" ); return ; } j--; i++; } System. out .println( "YES" ); } }

Finding the seat opposite to you in a train.

package ae.naveed; import java.util.Scanner; public class SeatingArrangement { enum seatPositions { WS , MS , AS ; }; public static void main(String[] args) { Scanner scanner = new Scanner(System. in ); int testCases = Integer. parseInt (scanner.nextLine()); int [] seatNumberArray = new int [testCases]; for ( int i = 0 ; i < testCases; i++) { seatNumberArray[i] = scanner.nextInt(); } for ( int i = 0 ; i < testCases; i++) { System. out .println( getOppositeSeat (seatNumberArray[i])); } } public static String getOppositeSeat ( int seatNumber) { int seatNumberFactored = seatNumber % 12 ; switch (seatNumberFactored) { case 1 : return "" + (seatNumber + 11 ) + " " + seatPositions. WS ; case 2 : return "" + (seatNumber + 9 ) + " " + seatPositions. MS ;

Code for finding all prime number under a given number N

I will be using the Sieve of Eratosthenes algorithm as mentioned on the wiki. The code is like this: import java.util.Arrays; import java.util.Scanner; public class FindingPrime { public static void main(String[] args) { Scanner scanner = new Scanner(System. in ); int n = Integer. parseInt (scanner.nextLine()); boolean [] a = new boolean [n]; Arrays. fill (a, true ); int squareRoot = ( int ) Math. ceil (Math. sqrt (n)); for ( int i = 2 ; i < squareRoot; i++) { if (a[i]) { int square = i * i; for ( int j = square; j < n; j = (j + i)) { a[j] = false ; } } } for ( int i = 2 ; i < a. length ; i++) { if (a[i]) { System. out .print(i + " " ); } } } }