Why this program is giving wrong output for palindrome? [duplicate] - java

This question already has answers here:
Why does a Java if statement fail when it ends in semicolon [duplicate]
(9 answers)
Closed 7 years ago.
I am making the program forthe largest palindrome made from the product of two 3-digit numbers.
But program is giving product of 999*999=998001
Can anyone tell what's wrong in this code?
Program-
public class abc {
public static void main(String[] args)
{
int p=0,temp=0;
for(int i=100;i<=999;i++)
{
for(int j=100;j<=999;j++)
{
p=i*j;
StringBuilder sb=new StringBuilder(Integer.toString(p));
sb.reverse();
if((sb.toString()).equals(Integer.toString(p)) && p>temp)
{
temp=p;
}
}
}
System.out.println(temp);
}
}

You have an extra ; :
if((sb.toString()).equals(Integer.toString(p)) && p>temp);
^
It ends the if statement, so the following block, with temp=p; is always executed.
Remove it and you'll get
906609

Related

Can someone please explain the working of for loop in this snippet [duplicate]

This question already has answers here:
What is a debugger and how can it help me diagnose problems?
(2 answers)
Closed 1 year ago.
public class Main {
public static void main(String[] args) {
permutationWithRepeation("PQR");
}
private static void permutationWithRepeation(String str1) {
System.out.println("The given string is: PQR");
System.out.println("The permuted strings are:");
showPermutation(str1, "");
}
private static void showPermutation(String str1, String NewStringToPrint) {
if (NewStringToPrint.length() == str1.length()) {
System.out.println(NewStringToPrint);
return;
}
for (int i = 0; i < str1.length(); i++) {
showPermutation(str1, NewStringToPrint + str1.charAt(i));
}
}
}
I am not able to understand after PPP
The output of the code is
The given string is: PQR
The permuted strings are:
PPP
PPQ
PPR
PQP
PQQ
PQR
PRP
PRQ
PRR
QPP
QPQ
QPR
QQP
QQQ
QQR
QRP
QRQ
QRR
RPP
RPQ
RPR
RQP
RQQ
RQR
RRP
RRQ
RRR
The main idea is recursion. On each recursion level you put character at next position. Loop at each level is used to put all possible characters at next position and to go into next recursion level. When all characters are set your string has length 3 and recursion stops.

How do I print "*" x times in the same row, using methods? [duplicate]

This question already has answers here:
Print character multiple times [duplicate]
(5 answers)
Closed 2 years ago.
I'm assigned to write a program which prints (x) amount of "*" in a row.
I tried using the "*".repeat(amount) command, but the software wont accept it as a solution.
Also tried using for and while -loops, but I can't figure how to get the *'s be on the same row.
public class Tulostelua {
public static void main(String[] args) {
printStarts(3);
}
public static void printStart(int amount) {
}
}
You could either use print (which doesn't start a new line) or add the chars to a string and println that.
So if I understand your question correctly, you need something like this:
public void print(int amount) {
for(int i = 0; i < amount; i++) {
System.out.print('*');
}
}

Write something in output for limited time in java [duplicate]

This question already has answers here:
How to delete stuff printed to console by System.out.println()?
(15 answers)
Closed 3 years ago.
I’m trying to print a string, only for 1 second.
The first thing I looked for was a method for deleting a line of console - terminal, But don't find anything.
Then try to backspace 5 times for example for “hello” then print another string like “ ” to delete hello string, but I couldn’t find out how \b works for java.
Now I'm confused, how I actually can solve my problem.
Any idea for backspace?
Or deleting something that's already printed?
Yor can do it by writing carriage return character \r in equal number of the length of already printed string. It will place the cursor back but will not clear the line but as you will write new characters older will be cleared.
I'm not sure to exactly understand what you need, I hope this code will help:
class Main {
public static void main(String[] args) throws InterruptedException {
String text = "123456";
System.out.print(text);
Thread.sleep(1000);
backspace(text.length());
Thread.sleep(1000);
System.out.print(text);
Thread.sleep(1000);
arrowLeft(3);
Thread.sleep(1000);
System.out.print(text);
}
public static void backspace(int number){
for(int i=0; i<number; i++){
System.out.print("\b \b");
}
}
public static void arrowLeft(int number){
for(int i=0; i<number; i++){
System.out.print("\b");
}
}
}

Confused with "If statement" in Java [duplicate]

This question already has answers here:
Semicolon at end of 'if' statement
(18 answers)
Closed 4 years ago.
I got to code Java for an assignment where am I going wrong with this "if statement" surely if I enter anything but 10 in won't print out Variable information. Picture attached.
import java.util.Scanner;
public class JavaApplication7 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int code = 0;
code = input.nextInt();
if (code == 10); {
System.out.println(code);
}
}
}
Never mind I think I figured it out I was ending my statement to early or something this stuff is not very forgiving. Edit: Just saw snr answered haha thank you.

Why the value of i is not changing inside the for loop [duplicate]

This question already has answers here:
Post increment operator not incrementing in for loop [duplicate]
(5 answers)
Closed 8 years ago.
class Test
{
public static void main(String args[])
{
int i=1;
for(int j=0;j<=2;j++)
{
i=i++;
}
System.out.println(i);
}
}
Why in this question the value of i is printing 1.
i=i++; doesn't change i.
It increments i but then assigns the old value of i to i (since the post increment operator returns the old value of the incremented number).
Either write :
i++;
or
i=i+1;

Categories