1. Senior Member
Join Date
Oct 2010
Location
Newark,nj
Posts
111
Rep Power
0

## newbie guidance.

Good Day All!!! i want this program to read an interger , and print the sum of all positive #'s numbers within it. Im Stummped once again.

Java Code:
```import java.util.Scanner ;
public class Kush  {
public static void main(String[]args)
{
int j, holder;
Scanner b = new Scanner(System.in);
System.out.print("PLease enter a number : ");
j = b.nextInt();
holder = j;

}
}```

2. Senior Member
Join Date
Feb 2010
Location
Ljubljana, Slovenia
Posts
470
Rep Power
11
There are a few ways to acomplish this task. The first that springs to mind is the standard mathematical method, I'll just write it up in pseudocode:
Java Code:
```start loop
get the last digit with the mod operator
divide number by 10
if number is zero, stop looping
end loop```
Also, if you try to write this method recursively, it'll end up being 2 lines long.
Next, you could also input your number as a String, and convert it to an int one character at a time, thus removing the need for mathematics apart from summing. See the API for the String class if you decide to go that route, you'll find all that you need there.

3. Senior Member
Join Date
Oct 2010
Location
Newark,nj
Posts
111
Rep Power
0
Am i on the right path?

Java Code:
```public static void main(String[]args)  {

int sum = 0;
int number = 0;
int num1,num2,num3,num4,num5;
Scanner scan = new Scanner(System.in);
int j = scan.nextInt();

while ( number >= 0) {
num1 = j  % 10;
sum = num1;

System.out.println(num1);

}
}
}```

4. Senior Member
Join Date
Oct 2010
Location
Newark,nj
Posts
111
Rep Power
0
Well so far i have extracted all the number not sure as to what else i have to do...ANYWAYS HERES WHAT I HAVE SO FAR

Java Code:
```import java.util.Scanner;
import java.util.Random;
public class ne  {
public static void main(String[]args)  {

int sum = 0;
int number = 0;
int num1= 0;
int num12 = 0;
int num2= 0;
int num13 =0;
int num3 =0;
int num14 = 0;
int num4 =0;
int num15 =0;
int num5 = 0;
int num16 =0;
Scanner scan = new Scanner(System.in);
int j = scan.nextInt();

if  ( number >= 0) {
num1 = j  % 10;
num12 = j /10;

num2 = num12 % 10;
num13 = num12 /10;

num3 = num13 %10;
num14 = num13/10;

num4 =num14 % 10;
num15 = num14 /10;

num5 = num15 %10;
num16 = num15/10;

sum = 0 +  num1+num2+num3+num4+num5;
}
System.out.println(num1);
System.out.println(num2);
System.out.println(num3);
System.out.println(num4);
System.out.println(num5);

}
}```

5. Senior Member
Join Date
Feb 2010
Location
Ljubljana, Slovenia
Posts
470
Rep Power
11
Take a look at my pseudocode again, note the first line start loop!

6. Member
Join Date
Oct 2010
Location
Pondicherry
Posts
6
Rep Power
0
i guess this code would help u...
Java Code:
```public static void main(String[] args) {
int sum = 0;
int num = 0;
int n;
Scanner scan = new Scanner(System.in);
int j = scan.nextInt();
n = j;
while ( n > 0) {
num = n  % 10;
sum = sum + num;
n /= 10;
}

System.out.println(sum);
}```

7. Senior Member
Join Date
Oct 2010
Location
Newark,nj
Posts
111
Rep Power
0
im not sure how to put the whole thing in a loop. i know its the same calculations over 5 times but dont no how to translate it into a loop.Thx:confused:

8. Senior Member
Join Date
Feb 2010
Location
Ljubljana, Slovenia
Posts
470
Rep Power
11
Since karthikaa already posted the solution for you, I'll [sarcasm] help [/sarcasm] more by providing something you probably won't understand but will give you a good grade.
Java Code:
```public int sumOfDigits(int num) {
if(num == 0) return 0;
return num%10+sumOfDigits(num/10);
}```
In short, providing answers is not the way to help people in the long run. Not directed at Bgreen7887.

9. Senior Member
Join Date
Oct 2010
Location
Newark,nj
Posts
111
Rep Power
0
Solved thanks! Sorry if i dont always understand , on the first go around. I am a rookie, im Learning from my Mistake. I just want to get better. Appreciate all the HELP

10. Senior Member
Join Date
Feb 2010
Location
Ljubljana, Slovenia
Posts
470
Rep Power
11
Like I said, the derision in my post wasn't aimed at you, but at the guy who posts solutions. Getting to grips with the basics is what gives you a good foundation for learning more advanced topics, someone spoonfeeding you solutions is only detrimental to your learning process.

11. Senior Member
Join Date
Oct 2010
Location
Newark,nj
Posts
111
Rep Power
0
I understood and agree with you. I've been told that programming is not what you but is how you think..Is there any truth to that statement?

12. Senior Member
Join Date
Feb 2010
Location
Ljubljana, Slovenia
Posts
470
Rep Power
11
Yes, pretty much. Learning to program has a lot to do with translating your thought process into an algorithm the computer can understand, regardless of the language you're learning. Also, each problem you face, try to solve it in a few different ways, and compare the results. Do all the methods return the same result (ie all function equivalently), how about the speed at which your algorithm solves a certain problem? Toying around like this is what gets you into the state of mind of a programmer.

13. Senior Member
Join Date
Oct 2010
Location
Newark,nj
Posts
111
Rep Power
0
Ok because i've been programming for about a month. I just started school for it. Sometime the hardest problem for me is to understand exactly what the question are asking for and then getting started! I know i give the time because programming is literally the only thing do now. I just can't see any huge improvements in myself since then .

14. Senior Member
Join Date
Feb 2010
Location
Ljubljana, Slovenia
Posts
470
Rep Power
11
A good example of tunnel vision solutions is your code snippet. You made a program that can sum the digits of a 5 digit number. What about 6, 7 and more digits? A similar example would be, to make a program that computes the 10th link of a fibonacci sequence. Are you going to make an algorithm that can find only the 10th element just so you can finish an assignment, or are you going to make an algorithm that can compute the n-th element and be done with the problem forever?
In summation, do many problems, try to find a generic solution that works on many cases, not just the one present in the assignment. Once you're more confortable with the basics, definately check out recursion. It's the one thing the really opened my eyes, and I've been making leaps in my learning process since.
Last edited by m00nchile; 10-17-2010 at 08:00 PM.

15. Senior Member
Join Date
Oct 2010
Location
Newark,nj
Posts
111
Rep Power
0
LOL your last reply made me laugh , but hit home your totally right. im not sure if this is normal but i usually get stumped on every code. maybe i'll go back to first chap of my book doing all the programming projects and increment like that. until im concurrent. :)

16. Senior Member
Join Date
Feb 2010
Location
Ljubljana, Slovenia
Posts
470
Rep Power
11
Hey, noone is born with all the knowledge in the world. Just keep plugging away, it'll all start to make sense soon :D

17. Originally Posted by m00nchile
Hey, noone is born with all the knowledge in the world.
Indeed, I was born stupid too but I fell on my head when I was a baby; from then on my mother told me that I wasn't a normal person ;-)

kind regards,

Jos

18. Senior Member
Join Date
Feb 2010
Location
Ljubljana, Slovenia
Posts
470
Rep Power
11
"Special" can go both ways :D

19. Senior Member
Join Date
Oct 2010
Location
Newark,nj
Posts
111
Rep Power
0
you guys are crazy i'd say i was weird not "special" lol ..why am i getting compile error with this so far?
Java Code:
```import java.util.Scanner;
public class ap  {
public static void main(String[]args)  {

String n;
Scanner jennifer = new Scanner(System.in);
n = jennifer.nextLine();
while (jennifer.hasNext() ) {
System.out.println(jennifer.nextLine);
}

}
}   // shouldn't it print user text on sentence per line```

Page 1 of 2 12 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•