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.
Asked
Active
Viewed 1,755 times
-4
-
5Have you done some effort to solve this?, Kindly show your efforts. – Tayyab Amin Oct 13 '18 at 05:38
-
1https://stackoverflow.com/questions/3806126/java-reverse-an-int-value-without-using-array reverse a number & then add it to the original – Niraj Chauhan Oct 13 '18 at 05:40
-
You can use StringBuilder too. – suvojit_007 Oct 13 '18 at 06:07
3 Answers
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.

Hugo Andérica
- 1
- 3
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