Link to home
Start Free TrialLog in
Avatar of Fred_45559
Fred_45559

asked on

Reverse a 4-digit number

Can you give an example of how to write a program to input a 4-digit number from the keyboard, find the reverse of the number and add the two.   The example is If the original number is 1234, then the reverse is 4321.
Avatar of F. Dominicus
F. Dominicus
Flag of Germany image

Where's your code? What did you try? Have you tried anything at all?

Regards
Friedrich
Avatar of sciuriware
sciuriware

HOMEWORK!
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;

// reverseInput is the java filename and class name
public class reverseInput extends Applet implements ActionListener {
      
      // create some user interaction
      JTextField userInput;
      JButton go;
      
      // setup interface
      public void init() {
      
            setLayout(null);
            
            userInput = new JTextField();
            add(userInput);
            userInput.setBounds(10,10,200,20);
            
            go = new JButton("Go");
            add(go);
            go.setBounds(10,40,50,20);
            go.addActionListener(this);
            
      }
      
      // create your reverse method
      public void reverse() {
      
            if(userInput.getText() != "") {
                  
                  String input = userInput.getText();
                  System.out.println(input);
                  
                  String temp = new StringBuffer(userInput.getText()).reverse().toString();
                  
                  userInput.setText(input + " " + temp);
            }
                        
      }
      
        // detect user action and perform the reversal
      public void actionPerformed (ActionEvent e) {
            
            if (e.getSource() == go) {
                  
                  reverse();
                  
            }
            
      }
            
}
Hi Fred,
We're not allowed to write all the code for you.
Show us what you already have and then we'll be able to help you further

// FIRST WAY WITHOUT USING REVERSE METHOD
import java.io.DataInputStream;
import java.io.IOException;
//without using any reverse method()
public class ReverseNo {

      public static void main(String[] args) {
            int Number = 0;
            String x="";
            System.out.println("Enter Integer Number");
            DataInputStream in=new DataInputStream(System.in);
            try {
             Number=Integer.parseInt(in.readLine());
            } catch (NumberFormatException e) {
                  
            } catch (IOException e) {
                  }
            
            if(Number<0)
            {
                  System.exit(0);
            }
            do
            {
            int No=Number%10;
            x=x+No;
          Number=Number/10;
            }while(Number>0);
          
       System.out.println("Reverse NO is-->>"+Integer.parseInt(x));    
            
      }

}

ANOTHER WAY 2:


//IF YOU WANT USE REVERSE METHOD
//1.CONVERT STRING TO STRING BUFFER
//2.USE REVERSE METHOD
//3.AFTER THAT CONVERT TO TO THE INTEGER


Avatar of Fred_45559

ASKER

I tried this code and could not get this code to compile.  

import java.util.*;
class Tester9
{
public static void main(String[]args)
{
int number,x;
int sum=0;
int reverse number;
Scanner input = new Scanner(System.in);
System.out.println("Please type a four-digit number for x");
number = input.nextInt();
number = x;
digit = x%10;
sum += digit;
x = x/10;
digit = x%10;
sum += digit;
x = x/10;
digit = x%10;
sum += digit;
x = x/10;
digit = x%10;
sum += digit;
reverse += digit;
System.out.printf("Given Number = %6d"+="reverse of digits=%6d"," and the sum of given and reverse digits=%6d",number,reverse,sum);
}
}


Fred

Fred,
your code contains lot of syntax error.Four times you are repeating the same code.
No need to repeat the code(use while loop,fo loopr etc...)

refer this code.

import java.io.DataInputStream;
import java.io.IOException;
public class ReverseNo {

      public static void main(String[] args) {
            int OrginNo=0;
            int sum=0;
            int Number = 0;
            String x="";
            System.out.println("Enter Integer Number");
            DataInputStream in=new DataInputStream(System.in);
            try {
             Number=Integer.parseInt(in.readLine());
             OrginNo=Number;
            } catch (NumberFormatException e) {
                  
            } catch (IOException e) {
                  }
            do
            {
            int No=Number%10;
            x=x+No;
            sum=sum+No;
          Number=Number/10;
            }while(Number>0);
          
            System.out.println("Given Number = "+OrginNo+" Reverse No is="+ x+"   Sum of given reverse digit is="+sum);
   
            
      }

}

Thanks

>> could not get this code to compile
Normally you get compiler errors indicating what is wrong.

E.g. You use a variable digit but did nowhere define it.

Remarks:
* you don't need the import.
* use good names for your variables. A variable with the name 'x' doesn't say much to the reader of your code

I would write it like this:

public class Tester9 {

    public static void main(String[]args) {
        int digit=0;
        int reverse = 0;
        Scanner input = new Scanner(System.in);
        System.out.println("Please type a four-digit number");
        int given = input.nextInt();
        int number = given;
        int factor = 1000;
        do {
            digit = number>10 ? number%10 : number; // when number>10, the digit is number/10,
                                                                            // otherwise the digit IS number
            reverse += digit*factor;  // add digit*factor to become the reverse number
            number /= 10;   // divide the number by 10 for the next iteration
            factor /= 10;      // divide the factor by 10 for the next iteration
        } while (factor != 0);
       
        System.out.println("Given Number = " + given + " - Reverse = " + reverse + " - Sum of both = " + (given+reverse));
    }

}
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This program worked for me


import java.util.*;
class Week4pro2dem
{
    public static void main(String[]args)
           {
        Scanner input = new Scanner(System.in);
        System.out.println("Please type a four-digit number");
        int original = input.nextInt();
        int reverse = Integer.parseInt( new StringBuffer( ""+original ).reverse().toString() );
        System.out.println("If the original Number is = " + original + " - Then the reverse of it is = " + reverse + " - Their Sum is = " + (original+reverse));
Fine.
Thanx 4 axxepting