Jump to content

PLZ help with" Return the reversal of an integer"

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
9 replies to this topic

#1
juice33

juice33

    Newbie

  • Members
  • Pip
  • 5 posts
I'm a little stuck in regards to writing code to revers the integer from the interger in putted. PlZ help 
import java.util.Scanner;


public class Palindrome{

	/**Main Method */

	public static void main(String[] args)

	{

    //Declare variables

	

	    int num1, num2, num3,num4,num5;

		

		//Create  a scanner

		Scanner readinput = new Scanner(System.in);

		// Introduce program

		System.out.print(" This is a Palindrome excercise: ");

		//Prompt user to enter an integer

		System.out.print("Enter 1st integer: ");

        num1 = readinput.nextInt();

		System.out.print("Enter 2nd integers: ");

		num2 = readinput.nextInt();

		System.out.print("Enter 3rd integers: ");

		num3 = readinput.nextInt();

		System.out.print("Enter 4th integers: ");

		num4 = readinput.nextInt();

		System.out.print("Enter 5th integers: ");

		num5 = readinput.nextInt();

		

		System.out.print("The integers entered are as follows:" +num1+"," +num2+ "," +num3+ "," +num4+ ","+num5 );

		}

	

	 // Return the reversal of an integer, i.e. reverse(456) returns 654   

	public static int  reverse (int num){

	for (int i = 0; i < 5; i++){

	}

	

	System.out.print("");

	}

}



#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
//This can't be done in a for loop unless you store the integers in an array.
int result =  (num5*10000) + (num4*1000) + (num3*100) + (num2 *10) + num1;

//OR
int result = Integer.parseInt( num5 + "" + num4 + "" + num3 + "" + num2 + "" + num1 )


#3
juice33

juice33

    Newbie

  • Members
  • Pip
  • 5 posts
How would i go by having the user to be prompted 5 times to enter digit

import java.util.Scanner;


public class Palindrome{

	/**Main Method */

	public static void main(String[] args)

	{

		//Create  a scanner

		Scanner readinput = new Scanner(System.in);

		// Introduce program

		System.out.print(" This is a Palindrome excercise: ");

		//Prompt user to enter an integer

		System.out.print("Enter an integer: ");

	

		int num = readinput.nextInt();

		int n=num;

		int rev =0;

		for (int i = 0; i < 5; i++) {  

		} 

		 System.out.println("The number entered is: ");

        System.out.println(" "+ num);

		for (int i=0; i<=num; i++){

          int r=num%10;

          num=num/10;

          rev=rev*10+r;

          i=0;

		  }

		 System.out.println("The number entered is now reversed: "+ " ");

        System.out.println(" "+ rev);      

        if(n == rev){

        System.out.print("The integers entered are Palindrome, True!");

        }

        else{

        System.out.println("The numbers is not palindrome, False!");

       }

    } 

 }        






#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
int[] numbers = new int[5] //Array of integers with 5 free spots.
for(int i=0 ; i<5 ; i++){
  System.out.println("Please enter the next number: ");
  numbers[i] = readinput.nextInt();
}
Then you have the 5 integers scanned in an array.

#5
juice33

juice33

    Newbie

  • Members
  • Pip
  • 5 posts
Im sorry im a beginner at this, i inputted the code that you supplied for the array but recived errors, were would i input this at

#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
I forgot a ';' at my first line.

It should be in the beginning:


                Scanner readinput = new Scanner(System.in);

                int[] numbers = new int[5];

                int reverse=0; //This will contain the reverse number later.


                System.out.print(" This is a Palindrome excercise: ");

 

                for (int i = 0; i < 5; i++) {  

                        System.out.println("Please enter the next number: ");

                        numbers[i] = readinput.nextInt();

                } 

Next up you have to loop trough the array, and reverse it, as explained in my first post.

Tip: this
int result =  (num5*10000) + (num4*1000) + (num3*100) + (num2 *10) + num1;
Can (and should) be done with a for-loop
assume the user entered 1,2,3,4,5

reverse += 1*numbers[0] is what happens in the first loop, (reverse = 1*1 = 1)
reverse += 10*numbers[1] in the second (reverse = reverse + 10*2 = 21),
reverse += 100*numbers[2] in the third... (reverse = reverse + 100*3 =321)
You see that reverse will eventually contain the reverse of the input.
Just put this in a for-loop

you'll need: import java.lang.Math
and use Math.pow(x, y);

2^3 = 8 = Math.pow(2, 8);

Think about it.

#7
juice33

juice33

    Newbie

  • Members
  • Pip
  • 5 posts
I wanted to make sure if this is what you meant by loop it through the array
import java.util.Scanner;


public class Palindrome{

	/**Main Method */

	public static void main(String[] args)

	{

	    Scanner readinput = new Scanner(System.in);

        int[] num = new int[5];

        int rev=0; //This will contain the reverse number later.


        System.out.print(" This is a Palindrome excercise: ");

 

        for (int i = 0; i < 5; i++) {  

        System.out.println("Please enter the next number: ");

        num[i] = readinput.nextInt();

		System.out.print(" "+""+""+""+"" );

	    }

 // Return the reversal of an integer, i.e. reverse(456) returns 654   

		public static int(int rev){

		int results = Integer.parseInt (num5 + "" + num4 + "" + num3 + "" + num2 + "" + num1 );

        System.out.print("");

		}

		if(num == rev){

        System.out.print("The integers entered are Palindrome, True!");

        }

        else{

        System.out.println("The numbers is not palindrome, False!");

	


#8
juice33

juice33

    Newbie

  • Members
  • Pip
  • 5 posts
like this right?
import java.util.Scanner;


public class Palindrome{

	/**Main Method */

	public static void main(String[] args)

	{

	    Scanner readinput = new Scanner(System.in);

        int[] num = new int[5];

        int rev=0; //This will contain the reverse number later.


        System.out.print(" This is a Palindrome excercise: ");

 

        for (int i = 0; i < 5; i++) {  

        System.out.println("Please enter the next number: ");

        num[i] = readinput.nextInt();

		System.out.print(" " );

	    }

 // Return the reversal of an integer, i.e. reverse(456) returns 654   


		for (int result =(num5*10000) + (num4*1000) + (num3*100) + (num2 *10) + num1)

		{

		System.out.printIn("");

		}

		if(num == rev){

        System.out.print("The integers entered are Palindrome, True!");

        }

        else{

        System.out.println("The numbers is not palindrome, False!");

		}

	}

}


#9
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Why not just do something like this?

public class Fun
{
    public static void main(String[] args)
    {
        int toReverse = 13531;
        if (args.length > 0)
        {
            // Too lazy to check it.
            toReverse = Integer.parseInt(args[0]);
        }

        String step1 = Integer.toString(toReverse);
        StringBuffer step2 = new StringBuffer(step1);
        String step3 = step2.reverse().toString();
        int otherNum = Integer.parseInt(step3);

        String lineToPrint = "The number " + step1 + " is ";
        if (toReverse != otherNum)
        {
            lineToPrint = lineToPrint + "NOT ";
        }
        System.out.println(lineToPrint + "a palindrome.");
    }
}

Wow I changed my sig!

#10
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
Oh, i didn't know stringbuilder has a reverse method

I had this in my mind:
import Java.lang.Math;
import java.util.Scanner;

class Palindrome{
 
        public static void main(String[] args)
        {
                Scanner readinput = new Scanner(System.in);
                int[] numbers = new int[5];
                int reverse=0;

                System.out.print(" This is a Palindrome excercise: ");
 
                for (int i = 0; i < 5; i++) {  
                        System.out.println("Please enter the next number: ");
                        numbers[i] = readinput.nextInt();
                } 
 
                for (int i=0 ; i<5; i++){
                        reverse += java.lang.Math.pow(10,i)*numbers[i];
        
                }
                System.out.println("The number entered is now reversed: "+ reverse);            
        } 
}

Or you can do it the easy was like ZekeDragon :D