Separating the Digits in an Integer - exercise from Deitel's Java book

write a program that requests a positive four digit integer from the user and prints its digits
input a number from the user and output its digits one digit per line

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 consisting of five digits from the user, separates the number into its individual digits and prints the digits separated from one another by three spaces each. For example, if the user types in the number 42339, the program should print

4   2   3   3   9

Assume that the user enters the correct number of digits. What happens when you enter a number with more than five digits? What happens when you enter a number with fewer than five digits?[Hint: It's possible to do this exercise with the techniques you learned in this chapter. You'll need to use both division and reminder operations to "pick off" each digit.]

Could someone explain to me how should I go about "picking off" individual integers using division and reminder operators?

EDIT: control structures (if / else and the like) are not allowed yet, those are explored in future chapters. Variables, arithmetic operators and comparison operators only.

You know that the number is 5 digit long.

What about

number / 10 000 to retrieve the first digit.
number = reminder
number / 1000 to retrieve the second digit.
number = reminder
number / 100 to retrieve the third digit.
number = reminder
number / 10 to retrieve the fourth digit.
and reminder is the 5th one.

Summing a number's digits in Java, Separating the Digits in an Integer - exercise from Deitel's Java book (8 answers) int i = 75; int a = i / 10; // 7 int b = i % 10; // 5 int c = a + b; // 12. A palindrome is a number or a text phrase that reads the same backward as forward. For example, each of the following five-digit integers is a palindrome: 12321, 55555, 45554 and 11611. Write a program that reads in a five-digit integer and determines whether or not it’s a palindrome.

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.

java: Separating the Digits in an Integer, 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 consisting of  // (Separating Digits in an Integer) Write a program that inputs one five-digit // number that separates the number into its individual digits and prints the // digits separated from one another by three spaces each.

Try this:

import java.util.*;
public class DigitsDisplay
{
    public static void main (String[] args)
    {
        Scanner input = new Scanner (System.in);
        int digit;
        System.out.print("Enter a positive number: ");
        digit = input.nextInt();
        int power = 1;
        while (power <= digit) {
            power *= 10;
        }
        power /= 10;
        while (power > 0) {
            System.out.println(digit/power);
            digit %= power;
            power /= 10;
        }
    }
}

28. Program that inputs a five-digit integer, separates the integer into , Exercise. 28. Write a program that inputs a five-digit integer, separates the integer into its digits and prints them separated by three spaces each  receives an integer between 1 and 99999 and displays it as a sequence of digits, separating each pair of digits by two spaces. For example, the integer 4562 should appear as 4 5 6 2 Incorporate the methods into an application that inputs an integer and calls display-Digits by passing the method the integer entered. Display the results.

Try something like this

String str1 = "";
int a = 12345;
while(a>0)
{
  int b = a%10;
  str1 = str1 + String.valueOf(b+" ");
  a = a/10;
}
StringBuilder str = new StringBuilder(str1);
System.out.println(str.reverse());

Programming Problems: Digits of an Integer, [Hint: It's possible to do this exercise with the tech- niques you learned in this chapter. http://www.deitel.com/Books/C/VisualC2012HowtoProgram/tabid/3645​/Default.aspx To mathematically split each digit in an input:. (Separating the Digits in an Integer) Write an application that inputs one number consisting of five digits from the user, separates the number into its individual digits and prints the digits separated from one another by three spaces each. For example, if the user types in the number 42339, the program should print

I also just started with java and with this book, and this is the code that did the tric for me. Forgive me if i did something strange... :)

import java.util.Scanner;

public class SeparateNumber
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int nr, nr1, nr2, nr3, nr4, nr5;

        System.out.print("Enter a number with 5 digits: ");
        nr = in.nextInt();

        nr1 = nr / 10000;
        nr2 = (nr % 10000) / 1000;
        nr3 = ((nr % 10000) % 1000) / 100;
        nr4 = (((nr % 10000) % 1000) % 100) / 10;
        nr5 = (((nr % 10000) % 1000) % 100) % 10;

        System.out.printf("%d%s%d%s%d%s%d%s%d%n", nr1, " ", nr2, " ", nr3, " ", nr4, " ", nr5);
    }
}

LearnCPPDeitel/6_24_main.cpp at master · PaulDulac , My exercise programs for the C++ book that I'm using to learn the language and OOP Exercise 6.24 in Deitel int separated; // the sparated digit to print. Regarding efficiency - your code has linear complexity *, which is the best complexity one should hope for in this kind of problem. * Computing complexity is a big topic (you can see here for some extensive answers on the subject); to correctly compute it, you need to have a reasonable knowledge about the methods you use (like trim() and toCharArray()).

Java how to program 10th edition solution manual pdf, Using statements similar to the one shown earlier in this exercise, write an 2.30 (Separating the Digits in an Integer) Write an application that inputs one  My exercise programs for the C++ book that I'm using to learn the language and OOP practices - PaulDulac/LearnCPPDeitel

[PDF] Instructor's Manual, Harvey M. Deitel. Paul J. Deitel Using the statements you wrote in Exercise 2.5, write a complete program that calculates and prints the Write a separate application that inputs an encrypted four-digit integer and decrypts it to form the  C# Sharp Exercises: Display the individual digits of a given number. Last update on May 17 2019 13:01:14 (UTC/GMT +8 hours) Write a program in C# Sharp to display the individual digits of a given number using recursion.

Separating digits in an Integer, This exercise is pretty strange. It speaks about number and digits, but they don't matter at all. If an input validation is required, I'd go for if (s.matches("\\d{5}")) . I need something more like..any mumber of integers, it can consist of 2 digits or 10 digits. For example, if I enter 8272, the program should display 8 2 7 2 and if I key in 75602, it should display 7 5 6 0 2 and then finally get the sum.. But I need to know how to get it to output the individual digits.

Comments
  • Hint: It's possible to do this exercise with the techniques you learned in this chapter.
  • This is a new question. You should ask it using the 'Ask Question' button instead of putting this down as an answer.
  • Looks like an answer to me.
  • My intention was idd to answer. :) I know this is an older question but i thought i would be helpfull for other starters to see a possible full 'solution'. Additionaly i also wanted to know if i did something stupid... :)
  • First: Welcome to SO :) Now, please rethink your answer. For once it does not really answer what OP asked: "Could someone explain to me how should I go about(...)" since you don't explain. However if you think your posted code provides an answer, then maybe you could explain why and how? Have a look here to see how you could improve your answer. Thanks