Page 1
Page 1
Started By
Message

Java Question: Displaying an inputed integer in reverse

Posted on 2/9/15 at 12:15 pm
Posted by jeff5891
Member since Aug 2011
15761 posts
Posted on 2/9/15 at 12:15 pm
I know how to do this in a complicated way but for this intro class I need to write it with basic computation knowledge.

The Question: Write a program that reads a 4 digit integer and reverses all the digits in the integer. For example, if an integer is 9324, the output should be 4239.

is there a way to write this by maybe using the placement of the individual digits to reverse the integer?



the more advanced way:
quote:


import java.util.Scanner;

public class ReverseNumberExample
{

public static void main(String args[])
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter an interger:");
int number = keyboard.nextInt();

int reverse = reverse(number);
System.out.println("The reverse of the integer is" + reverse(number));

}

public static int reverse(int number)
{
int reverse = 0;
int remainder = 0;
do{
remainder = number%10;
reverse = reverse*10 + remainder;
number = number/10;
}

while(number > 0);

return reverse;
}

}

Posted by MamouTiger65
Baton Rouge, La
Member since Oct 2007
794 posts
Posted on 2/9/15 at 1:10 pm to
How about read each number into a character array and then print the array out in reverse.

Posted by jeff5891
Member since Aug 2011
15761 posts
Posted on 2/9/15 at 3:32 pm to
Using arrays is too advanced, apparently


I have to use sorting from chapter 2, which is just on assignments, index, basic string methods, ect
This post was edited on 2/9/15 at 3:42 pm
first pageprev pagePage 1 of 1Next pagelast page
refresh

Back to top
logoFollow TigerDroppings for LSU Football News
Follow us on Twitter, Facebook and Instagram to get the latest updates on LSU Football and Recruiting.

FacebookTwitterInstagram