-4

I have to solve the following problem:

-find flowchart or algorithm and show it in java languange

"Write a Java program that prompts the user to input a four-digit positive integers between 1001 and 9999. The program then finds the reverse of that integers. For example, if the input integer i2 3245, its reverse is 5423."

My problem is that I don't know what formula to use. I have also asked my demonstrator, but he just said that the formula uses a percentage and divide. How should I approach a solution to this problem?

Bart
  • 19,692
  • 7
  • 68
  • 77
farizi
  • 13
  • 3
  • So I guess he wants you to use integer manipulation, and not string manipulation? How far have you gotten? How would you approach this. Even if not in Java, did you get something in pseudo-code? If so, show it to us and demonstrate where you are stuck in that process. (p.s. in stead of percentage, I assume he means a "modulo". Perhaps look that up and see if it makes sense). – Bart Oct 05 '13 at 13:43
  • 1
    possible duplicate of [Java reverse int value](http://stackoverflow.com/questions/3806126/java-reverse-int-value) – Naveen Kumar Alone Oct 05 '13 at 13:44

2 Answers2

3

Since this is a learning assignment, I will give you only hints:

  • To get the last digit in base N, use x % N; for base ten, that would be x % 10
  • To drop the last digit in base N, integer-divide by N; for base ten, that would be x /= 10

Repeating this process four times and printing the digits in reverse order will give you the desired result. Since you know that the value has exactly four digits, you do not need a loop.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

This might not be what the teacher accepts/wants/expects, but for educational purposes, this would be the easiest way to do it:

String input = "1234";
int result = Integer.parseInt(new StringBuilder(input).reverse().toString());
System.out.println(result):

prints

4321
Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111