-4

My question is based on a number manipulation in java. please, give any example for calculating the sum of any numbers and its reverse in java.for example, 123+321.

3 Answers3

0
Public int sumReverse(int num){
    int orignal=num;
    int reverse=0;
    While(orignal>0){

        int remainder=orignal%10;
        reverse=reverse*10+remainder;
        orignal=orignal/10;

    }
    return num+reverse;
}
Khalid Shah
  • 3,132
  • 3
  • 20
  • 39
0
public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        String n2s="";
        int n1= sc.nextInt();
        String n1s= String.valueOf(n1); //let n1s be the String of n1
        for (int i=1;i<=n1s.length();i++){
            n2s+=n1s.charAt(n1s.length()-i); 
        }
        System.out.println(n2s);
        int n2=Integer.valueOf(n2s);
        int adit=n1+n2;
        System.out.println(n1s+" + "+n2s +" = "+ adit);
    }

In order to manipulate numbers they way you're looking for, it is easier to work that answer with the String of that number. Scanner allow the user to type any number he/she wants to manipulate. Hope this is usefull for you.

0

You could use StringBuilder also

int num = 123;
StringBuilder ob = new StringBuilder(Integer.toString(num));
ob.reverse();
System.out.println(num + Integer.valueOf(ob.toString()));

Store the data in StringBuilder and then add it to the original after reversing it.

suvojit_007
  • 1,690
  • 2
  • 17
  • 23