How can we make a Java program that reverses a given five digit integer?
January 21, 2012 – 5:36 pmhow do we program Java that accepts a five-digit integer into its individual digits and prints the digits, in reverse, separated from one another by three spaces each.
for example, if the user types 78906 the program should print 60987.
pls help, it’s my first time taking up a programming subject.
Related posts:
- how do i can write a program in java which for any given positive integer N, count digits?
- How do you seperate a three-digit number in java?
- How to count evens odds and zeros in a given integer by JAVA?
- How to count evens odds and zeros in a given integer by JAVA?
- How to count evens odds and zeros in a given integer by JAVA?
- How do I add the first and second digit of said number in python?
- Writing a Java program to compute the power of a number?
- How do you program java to read integer representing the number of floating point numbers,?
- How to swap digits in a two-digit number in java?
- Can somebody please help me learn to use Java strings for a Mad Libs program?
2 Responses to “How can we make a Java program that reverses a given five digit integer?”
If you input the number as a string then there are methods to convert a string to an array of chars.
If you input the number as an integer then use the modulus operator (%)
int num = 78906;
int digit = num % 10; // digit is 6
num /= 10; // Integer division, num is 7890
Have fun.
By AnalProgrammer on Jan 21, 2012
package reversednumbers;
import java.util.*;
public class Main {
public static void main(String args[])
{
int op;
Scanner Input= new Scanner(System.in);
System.out.println("Input 5 digit number:");
op=Input.nextInt();
System.out.println();
for (int ctr=4;ctr>=0;ctr–)
{
System.out.print(String.valueOf(op).charAt(ctr)+" ");
}
System.out.println();
}
}
created by axe cent add me on fb stonegiant08@yahoo.com
By Healthmaker E on Jan 21, 2012