# Negative Integers

• 09-30-2013, 07:59 PM
Aries.
Negative Integers
Code:

```package ZhyperMUFile; import java.util.*; public class javaprogram1{ public static void main(String []args){         Scanner scn=new Scanner(System.in); int nums, store, nums1,counter = 0 ; System.out.print("Enter a number: "); nums=scn.nextInt(); store = nums;         while (store > 0){   store  = store/10;   counter = counter + 1;         }         counter = counter - 1;         while (counter >= 0){                 nums1 = nums% (int) Math.pow(10, counter);                 nums = nums/(int) Math.pow(10,counter);                 System.out.print (nums + "\t");                 nums = nums1;                 counter = counter - 1;                 }         } }```
Output :

http://i.imgur.com/W1GRWbz.png

Inputting positive integers is fine, when I try to to input negative integers it doesn't show the answer.

• 09-30-2013, 08:30 PM
jim829
Re: Negative Integers

1. Input a negative number --> nums.
2. Assign nums to store.
3. Since store is now negative the while loop exits immediately. So counter remains 0.
4. Now counter becomes -1.
5. Since -1 is less than 0 your second while loop exits immediately

Regards,
Jim
• 09-30-2013, 10:10 PM
gimbal2
Re: Negative Integers
follow the logic, or add System.out.println statements to do it for you.
• 10-01-2013, 01:41 AM
Aries.
Re: Negative Integers
I got it guys, few question.

Is it possible to remove the (-) sign?

Example :

Input : -123
Output : 1 2 3

with spacing.

Thanks for guide.
• 10-01-2013, 02:53 AM
jim829
Re: Negative Integers
Well, if you want to. Just take the absolute value of the entered number - Math.abs().

Regards,
Jim
• 10-02-2013, 02:15 AM
Aries.
Re: Negative Integers
Quote:

Originally Posted by jim829
Well, if you want to. Just take the absolute value of the entered number - Math.abs().

Regards,
Jim

Done,

Only the spacing that I can't get for negative integers, any idea?
• 10-02-2013, 03:56 AM
Junky
Re: Negative Integers
Huh?

The spacing is exactly the same for negative and positive numbers. It all depends upon what you put in the print statements and where you place them.
• 10-03-2013, 12:16 AM
Aries.
Re: Negative Integers
One thing I forgot,

When I input negative integers

it display like this

Input : -123
Output : 12300

or can someone modify my code above? or just put the code and I fill up.

System.__.print ("__");

just like that it seems this forum isn't a spoonfeeding.

Thanks.
• 10-03-2013, 01:26 AM
jim829
Re: Negative Integers
That doesn't make sense. The first thing you should do is take the absolute value. Then it should behave as though you entered a positive number.

Regards,
Jim
• 10-03-2013, 05:45 AM
Aries.
Re: Negative Integers
Quote:

Originally Posted by jim829
That doesn't make sense. The first thing you should do is take the absolute value. Then it should behave as though you entered a positive number.

Regards,
Jim

Still don't get it . Sigh ----
• 10-03-2013, 05:51 AM
Junky
Re: Negative Integers
I assume your program currently works for positive numbers. Is that correct?
If so you program should be something like:
get user input
process input
produce output

What you need to do is insert an extra step to check if the input is negative and convert to positive. Where do yo think that step should go? Before input? After output? Somewhere else?
• 10-04-2013, 01:12 AM
Aries.
Re: Negative Integers
Quote:

Originally Posted by Junky
I assume your program currently works for positive numbers. Is that correct?
If so you program should be something like:
get user input
process input
produce output

What you need to do is insert an extra step to check if the input is negative and convert to positive. Where do yo think that step should go? Before input? After output? Somewhere else?

what I mean is, what line should I insert the code for negative integers to make it process until the displaying output. Yes the program is for positive only. I just saw this problem from .pdf file where I downloaded and I wanted to solve.
• 10-04-2013, 04:18 AM
Junky
Re: Negative Integers
You have already been given the answer in reply #5. Convert a negative number into a positive number before processing it. Are you going to try or continue whining hoping someone will just give you the code?
• 10-06-2013, 12:44 AM
Aries.
Re: Negative Integers
Quote:

Originally Posted by Junky
You have already been given the answer in reply #5. Convert a negative number into a positive number before processing it. Are you going to try or continue whining hoping someone will just give you the code?

i'm not whining in the first place, i came here for learning. isn't a big deal?
• 10-07-2013, 06:10 AM
Aries.
Re: Negative Integers