Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java using palindrome, Please, give a full code with comments to understand it.

ID: 3844167 • Letter: J

Question

Java using palindrome, Please, give a full code with comments to understand it.

Write a method named palindromeAddNumber in Java that accepts an integer parameter and repeatedly adds the integer to the reversal of itself (the integer constructed by reversing the order of the digits) until the result is a palindrome (an integer that is the same when the order of its digits is reversed). Your method should print a single line of output containing the number of adds that were needed, followed by a space, followed by the palindrome that was computed. For example, the ca of palindromeAddNumber(195); should print: 4 9339 because:

Explanation / Answer

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;
class Test
{
static int reverse(int n)// A method which returns the reverse value of the given no.
{
int r=0;// r is a variable that stores the reverse no.
for(int i=n;i>0;i=i/10)
{
int d=i%10;// d stores the extracted digit from the given no.
r=r*10+d;//here no.is being stored in reverse order
}
return r;//reverse no. is retured here
}
static boolean pallindrom(int n)// A method to check whether the given no. is pallindrome or not
{
int res=reverse(n);// res variable stores the reversed no.
//Pallindrome condition will be checked in the subsequent code
if(res==n)
return true;
else
return false;
}
public static void pallindromeAddNumber (int n) //A method that will print the no.of addition required to get pallindrome and the pallindrome no.
{
int rn=reverse(n);//no. is reversed and stored in variable rn
int res=n+rn;//res stores the summation of no. and reverse no.
int c=0;//c variable will count the no. of addition requied to get pallindrome
while(pallindrom(res)!=true)//loop until lthe result obtained is pallindrome
{
rn=reverse(res);//no. is reversed and stored in variable rn
res=rn+res;//res stores the summation of no. and reverse no.
c++;//c variable will count the no. of addition requied to get pallindrome
}
System.out.println(c+1+" "+res);//Finally result is printed by incrementing the value of c by 1.It is because the no. of //count made by c during loop was 1 less than actual value
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
pallindromeAddNumber(n);
}
}

Output:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote