0
a)Write a function ‘rev’ which returns the reverse of the given integer. Function isPalin takes an integer as argument and checks whether it is palindrome or not using rev function. Using isPalin write a main function to accept n integers and display number of palindrome numbers found. b)write a program to find the possible permutations of the given word .
2 odpowiedzi
+ 2
http://code.sololearn.com/c4896s1fQ1zc
import java.util.Scanner;
public class Program {
public static int rev(int n) {
int res = 0;
while (n != 0) {
res = res*10 + n%10;
n = n/10;
}
return res;
}
public static boolean isPalin(int n) {
return (n == rev(n));
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Please enter how many numbers you want to check: ");
int n = sc.nextInt();
System.out.println(n);
System.out.println("Please enter the " + n + " numbers (separated by space)");
int count = 0;
for (int i = 0; i < n; i++) {
if (isPalin(sc.nextInt())) {
count++;
}
}
System.out.println(count + " palindrome(s) found.");
}
}
+ 2
Permutations:
http://code.sololearn.com/ceAgOb5QdtbP
import java.util.Scanner;
public class Program {
private static void printPerm_rec(String fixed, String toChooseFrom) {
int n = toChooseFrom.length();
if (n == 0) {
System.out.println(fixed);
} else {
for (int i = 0; i < n; i++) {
printPerm_rec(fixed + toChooseFrom.charAt(i), toChooseFrom.substring(0, i) + toChooseFrom.substring(i+1, n));
}
}
}
public static void printPerm(String s) {
printPerm_rec("", s);
}
public static void main(String[] args) {
System.out.println("Permutations");
Scanner sc = new Scanner(System.in);
System.out.print("Please enter a word: ");
String s = sc.nextLine();
System.out.println(s);
printPerm(s);
}
}