+ 1
Whats wrong in the code plis helo
public class Program { public static void main(String[] args) { System.out.println(Palindrome(373)); } } public class Pal{ int a,b,sum=0; static void Palindrome(int n){ b=n; while(n>0){ a=n%10; sum=(sum*10)+a; n=n/10; } if(sum==b){ System.out.println("palindrome"); } else{ System.out.println("not palindrome"); } } }
6 Antworten
+ 4
Please, post codes as links to Code Playground. It will make helping you easier for us.
+ 3
Thank you everyone
+ 2
1. The method Palindrome is inside another class called Pal, so to call it inside main use:
'Pal.Palindrome(373)'
2. The method Palindrome is accessing variables 'a', 'b' and 'sum',
because Palindrome is a static method this variables must be static:
static int a, b, sum = 0;
3. The method Palindrome returns void, so you should not put it inside a 'println' in main.
Just do this:
Pal.Palindrome(373);
+ 2
Add int a,b,sum=0; declarations inside method.
Moreover call the method Palindrome
using the class name and not inside in println.
For example :
Pal.Palindrome(373);
If you need to call it inside the println function you should make it return string.
The full code is as follow :
public class Pal{
static void Palindrome(int n){
int a,b,sum=0;
b=n;
while(n>0){
a=n%10;
sum=(sum*10)+a;
n=n/10;
}
if(sum==b){
System.out.println("palindrome");
}
else{
System.out.println("not palindrome");
}
}
}
public class Program
{
public static void main(String[] args) {
Pal.Palindrome(373);
}
}
+ 2
The other way is :
public class Pal{
static String Palindrome(int n){
int a,b,sum=0;
b=n;
while(n>0){
a=n%10;
sum=(sum*10)+a;
n=n/10;
}
if(sum==b){
return "palindrome";
}
else{
return "not palindrome";
}
}
}
public class Program
{
public static void main(String[] args) {
System.out.println(Pal.Palindrome(373));
}
}