+ 1
Write a program for palindrome number using for loop??only using for loop!
2 Réponses
0
public static void main(String[] args) throws IOException
{
System.out.println("Enter the no. !");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int number[] = new int[1];
number[0]=Integer.parseInt(br.readLine());
for (int i = 0; i < number.length; i++) {
int numberToCheck = number[i];
int numberInReverse = 0;
int temp = 0;
while (numberToCheck > 0) {
temp = numberToCheck % 10;
numberInReverse = numberInReverse * 10 + temp;
numberToCheck = numberToCheck / 10;
}
if (number[i] == numberInReverse) {
System.out.println(number[i] + " is a palindrome");
}
else {
System.out.println(number[i] + " is NOT a palindrome");
}
}
}
0
import java.util.Scanner;
class PalindromeNum{
public static void main(String[] args) {
System.out.println("Enter the number");
Scanner sc = new Scanner(System.in);
int input = Integer.parseInt(sc.nextLine());
System.out.println("Palindrome numbers between 0 to " + input + " are: ");
for (int i = 0; i <= input; i++) {
int num = i, number = 0, t = 0;
while(num >0) {
t = num % 10;
num= num / 10;
number = number * 10 + t;
}
if(i == number)
System.out.print(i +" ");
}
}
}