Results 1 to 3 of 3
  1. #1
    irnie1994 is offline Member
    Join Date
    Aug 2011
    Posts
    28
    Rep Power
    0

    Talking reversing number cant run negative number

    Java Code:
    import java.util.*;
    import java.lang.*;
    
    public class as
    {
    	static Scanner console = new Scanner (System.in);
      //prompts the user for input and calls necessary methods
      public static void main(String args[]){
      
     	int Input,a;
     	System.out.println("Input");
     	Input=console.nextInt();
     	a=Input;
     	System.out.println("Output"+a+" as ");
     	int len=String.valueOf(Input).length();
     	int [] digits = new int[len];
     	int i = 0;
     	while (Input != 0){
     		digits[i++] = Input % 10;
     		Input/=10;
     	}
     	for (i = 0; i <digits.length; i++){
     		System.out.print(digits[i]);
     	}
     	}
      }
    ---> if you input -5 the output is -50 ... why the output like that?? some code's miss?
    Last edited by Fubarable; 10-30-2011 at 06:53 PM. Reason: code tags added

  2. #2
    irnie1994 is offline Member
    Join Date
    Aug 2011
    Posts
    28
    Rep Power
    0

    Default Re: reversing number cant run negative number

    import java.util.*;
    import java.lang.*;

    public class as
    {
    static Scanner console = new Scanner (System.in);
    //prompts the user for input and calls necessary methods
    public static void main(String args[]){

    int Input,a;
    System.out.println("Input");
    Input=console.nextInt();
    a=Input;
    System.out.println("Output"+a+" as ");
    int len=String.valueOf(Input).length();

    int [] digits = new int[len];
    int i=0;

    while (Input != 0){
    digits[i++] = Input % 10;
    Input/=10;
    }

    for (i = 0; i < digits.length; ++i){
    digits[i] = (int)(Math.abs(digits[i]));
    System.out.print(digits[i]);
    }
    }
    }

    someone can help me fix???

  3. #3
    christopherx is offline Member
    Join Date
    Oct 2011
    Posts
    92
    Rep Power
    0

    Default Re: reversing number cant run negative number

    Look closely at your last for loop :)

Similar Threads

  1. Find the First Negative Number of an Array
    By Cod in forum New To Java
    Replies: 12
    Last Post: 02-20-2011, 06:59 PM
  2. Replies: 7
    Last Post: 09-28-2010, 01:17 AM
  3. Finding a number in array close to another number
    By SteroidalPsycho in forum New To Java
    Replies: 2
    Last Post: 02-15-2010, 01:37 AM
  4. Printing the Number of Times a Number in a Range Shows up
    By space4rent00 in forum New To Java
    Replies: 1
    Last Post: 02-05-2010, 11:42 PM
  5. [SOLVED] Array of first negative number
    By random0munky in forum New To Java
    Replies: 9
    Last Post: 12-09-2008, 12:17 AM

Posting Permissions

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