Results 1 to 4 of 4
  1. #1
    aramiky818 is offline Member
    Join Date
    Apr 2011
    Posts
    14
    Rep Power
    0

    Default Reverse the integers

    i have the following code and it compiles and works perfectly. But the project wants me too do the revers as well
    for example) if i inter 120, the program returns factors which are 2 2 2 3 5
    but i also need it to return the reverse 5 3 2 2 2
    the book wants me to use a StackOfIntegers class
    how would i do this...i need complete details please

    thank you

    public class factors {

    public static void main(String args[])
    {
    java.util.Scanner input = new java.util.Scanner(System.in);


    System.out.print(
    "Enter a positive integer: ");
    int number = input.nextInt();


    System.out.print("The factors for " + number + " is:");
    int factor = 2;
    while (factor <= number)
    {
    if (number % factor == 0)
    {
    number = number / factor;
    System.out.print(" "+factor);


    }
    else {
    factor++;
    }
    }
    }
    }

  2. #2
    snotmare is offline Member
    Join Date
    Apr 2011
    Posts
    34
    Rep Power
    0

    Default

    Sounds like you need to create your own StackOfIntegers class? Does it say how you need to create this class?

    If it were me, I'd just add all the factors to an ArrayList. Once in the array, loop through it forwards and print out your results, then loop a second time backwards and print out the results.

  3. #3
    YAY
    YAY is offline Member
    Join Date
    Feb 2011
    Posts
    20
    Rep Power
    0

    Default

    Hi, this works fine:
    Java Code:
    public class factors
    {
    	public static void main(String args[])
    	{
    		int[] facts = new int[100];
    		int i = 0, j = 0;
    		java.util.Scanner input = new java.util.Scanner(System.in);
    		System.out.print("Enter a positive integer: ");
    		int number = input.nextInt();
    		System.out.print("The factors for " + number + " is:");
    		int factor = 2;
    		while (factor <= number)
    		{
    			if (number % factor == 0) 
    			{
    				number = number / factor;
    				facts[i] = factor;
    				i++;
    			}
    			else factor++;
    		}
    		for (j = i - 1; j >= 0; j--)
    		{
    			System.out.print(" " + facts[j]);
    		}
    		
    	}
    }

  4. #4
    aramiky818 is offline Member
    Join Date
    Apr 2011
    Posts
    14
    Rep Power
    0

    Default

    yes, i know what you said works, but i need a program that does both at the same time, thats what im haivng trouble with. i can't seem to combine a program that says for example

    if you input 120,
    to show 5 3 2 2 2
    also revers: 2 2 2 3 5

Similar Threads

  1. How to reverse a string?
    By Neeer in forum New To Java
    Replies: 17
    Last Post: 03-27-2011, 08:10 PM
  2. Replies: 0
    Last Post: 02-15-2011, 11:16 PM
  3. Without using reverse method
    By blindfolded916 in forum New To Java
    Replies: 3
    Last Post: 12-08-2010, 06:18 PM
  4. Reverse a string?
    By cysquatch in forum New To Java
    Replies: 15
    Last Post: 03-23-2010, 02:31 AM
  5. How to reverse two dimensional
    By masaka in forum New To Java
    Replies: 4
    Last Post: 05-19-2008, 10:02 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
  •