You are using the method System.out.println
, which will print a new-line at the end. You can use System.out.print
, which won't do that.
Also, remember that you get the part "The digits of ... are: ..."
many times because it is inside the for
loop.
System.out.print("The digits of " + integer + " are: ");
for (int count = 0; count < Stringnum.length(); count++) {
// ...
System.out.print(digit + " "); // add space
}
System.out.println("\nThe sum of the digits = " + sum); // note new-line character '\n'
Note:
- You can add a new-line character inside the
String
you want to print with"\n"
.