- My Forums
- Tiger Rant
- LSU Recruiting
- SEC Rant
- Saints Talk
- Pelicans Talk
- More Sports Board
- Fantasy Sports
- Golf Board
- Soccer Board
- O-T Lounge
- Tech Board
- Home/Garden Board
- Outdoor Board
- Health/Fitness Board
- Movie/TV Board
- Book Board
- Music Board
- Political Talk
- Money Talk
- Fark Board
- Gaming Board
- Travel Board
- Food/Drink Board
- Ticket Exchange
- TD Help Board
Customize My Forums- View All Forums
- Show Left Links
- Topic Sort Options
- Trending Topics
- Recent Topics
- Active Topics
Started By
Message
Java Question: Displaying an inputed integer in reverse
Posted on 2/9/15 at 12:15 pm
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:
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 on 2/9/15 at 1:10 pm to jeff5891
How about read each number into a character array and then print the array out in reverse.
Posted on 2/9/15 at 3:32 pm to MamouTiger65
Using arrays is too advanced, apparently
I have to use sorting from chapter 2, which is just on assignments, index, basic string methods, ect
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
Popular
Back to top
Follow TigerDroppings for LSU Football News