# Thread: Negative Integers

1. Member
Join Date
Jan 2013
Posts
22
Rep Power
0

## Negative Integers

Java 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 :

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

2. Senior Member
Join Date
Jan 2013
Location
United States
Posts
2,684
Rep Power
4

## Re: Negative Integers

You just need to follow the logic of your program.

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

3. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
2,610
Rep Power
3

## Re: Negative Integers

follow the logic, or add System.out.println statements to do it for you.

4. Member
Join Date
Jan 2013
Posts
22
Rep Power
0

## 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.
Last edited by Aries.; 10-01-2013 at 02:43 AM.

5. Senior Member
Join Date
Jan 2013
Location
United States
Posts
2,684
Rep Power
4

## Re: Negative Integers

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

Regards,
Jim

6. Member
Join Date
Jan 2013
Posts
22
Rep Power
0

## Re: Negative Integers

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?

7. ## 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.

8. Member
Join Date
Jan 2013
Posts
22
Rep Power
0

## 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.

9. Senior Member
Join Date
Jan 2013
Location
United States
Posts
2,684
Rep Power
4

## 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. Member
Join Date
Jan 2013
Posts
22
Rep Power
0

## Re: Negative Integers

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 ----

11. ## 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?

12. Member
Join Date
Jan 2013
Posts
22
Rep Power
0

## Re: Negative Integers

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.

13. ## 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?

14. Member
Join Date
Jan 2013
Posts
22
Rep Power
0

## Re: Negative Integers

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?

15. Member
Join Date
Jan 2013
Posts
22
Rep Power
0

## Re: Negative Integers

I already got it, -_-

/closed

#### Posting Permissions

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