Exercise from Deitel\'s \"Java How To Program\" 10th edition:
2.30 (Separating the Digits in an Integer) Write an application that inputs one number c
Here is the answer of displaying digit in same order u have enter
import java.util.Scanner;
public class oddMethod {
public static void main(String[] args) {
System.out.println("Enter start number:");
int number=getNumber();
int last=Reverse1stTime(number);
System.out.println("Digit You Enter:"+last);
}
private static int getNumber() {
Scanner sr = new Scanner(System.in);
return sr.nextInt();
}
private static int Reverse1stTime(int number)
{ int digit=0;
int reverse=0;
while(number>0)
{
digit=number%10;
reverse=reverse*10+digit;
number=number/10;
}
return reverseAgain(reverse);
}
private static int reverseAgain(int number)
{ int digit=0;
int reverse=0;
while(number>0)
{
digit=number%10;
System.out.println(digit);
reverse=(reverse*10+digit);
number=number/10;
}
return reverse;
}
}
Input 12345
Output 12345
package chapter1;
import java.util.Scanner;
public class Exercise3 {
public static void main (String[] args){
Scanner scan = new Scanner (System.in);
System.out.print ("Enter a number that consist of 5 digits: ");
int num =scan.nextInt();
int digit1 =num / 10000;
int digit2 =(( num % 10000) / 1000 ); // the modulus answer of num/10000 is divided by 1000
int digit3 =( ( num % 1000) / 100 );
int digit4 = ( ( num% 100 ) / 10 );
int digit5 = ( num % 10);
System.out.print (digit1+"\t");
System.out.print(digit2+"\t");
System.out.print(digit3+"\t");
System.out.print(digit4+"\t");
System.out.println(digit5+"\t");
}
}
my small hint with division - you can divide by 10 and take a look, what is that operation giving... More, you have to deduce if you want to learn programming.
import java.util.Scanner;
public class DigitSeparator{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter a 5 digit number: ");
int number = input.nextInt();
System.out.print((number / 10000)+ " ");
int divider = 10000;
int mod = number % 10000;
for(int i = 1; i <= 4; i++){
divider /= 10;
System.out.print( mod/divider +" ");
mod %= divider;
}
System.out.println();
}
}