Welcome to Dream.In.Code
Become a Java Expert!

Join 149,502 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,378 people online right now. Registration is fast and FREE... Join Now!




sums number, print one line at a time

 
Reply to this topicStart new topic

sums number, print one line at a time, Need help please

wavyaquaeyes
27 Mar, 2007 - 02:17 PM
Post #1

New D.I.C Head
*

Joined: 12 Dec, 2005
Posts: 7


My Contributions
[b]Hi All,
I am summing the numbers, then need to print each number on a seperate line. For instance if i input 1234 on each separate line it should add it, give the total such as 10, then print
1
2
3
4

I am getting 2 of the numbers to print correctly. Can someone help me please. I also have to do one in ascending order.

Code is

import java.util.*;
public class sumspg296
{
static Scanner console = new Scanner(System.in);
public static void main (String[] args)
{
// Program to ask user to input digits returns the digits and sums */

int num, temp, sum, ans;

System.out.print("Enter A Number: ");
num = console.nextInt();
ans = num % 10;
System.out.println();

temp = num;

sum = 0;
do
{
sum = sum + num % 10;

num = num / 10;

} while (num >= 0);
System.out.println("The sum of the numbers = " + sum);
num = num / 10;
ans = num % 10;
System.out.println(" " + ans);
num = num / 10;
ans = num % 10;
System.out.println(" " + ans);




}
}


Thanks


User is offlineProfile CardPM
+Quote Post

spullen
RE: Sums Number, Print One Line At A Time
27 Mar, 2007 - 02:37 PM
Post #2

D.I.C Regular
Group Icon

Joined: 22 Mar, 2007
Posts: 330



Thanked: 1 times
Dream Kudos: 50
My Contributions
You could cast the number to a string and then just do stringtokenizer or substring and print out each one that way.
User is offlineProfile CardPM
+Quote Post

capty99
RE: Sums Number, Print One Line At A Time
27 Mar, 2007 - 03:30 PM
Post #3

the real kya
Group Icon

Joined: 26 Apr, 2001
Posts: 9,252



Thanked: 16 times
Dream Kudos: 550
My Contributions
check out this thread for a couple of different options :

http://www.dreamincode.net/forums/showtopic24618.htm

if you need more help after that let us know.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 06:40PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month