1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
public class TTest {
public static void main(String[] args){
int r=0;
int a;
StringBuffer n1 = new StringBuffer("1234567");
StringBuffer n2 = new StringBuffer("1432094");
StringBuffer n = new StringBuffer();
n1.reverse();n2.reverse();// inverser les 2 chaines
for(int i=0;i<n1.length() && i< n2.length();i++){
a = Integer.parseInt(n1.substring(i,i+1)) + Integer.parseInt(n2.substring(i,i+1))+r; // AJOUT DE r
r=a/10;
a=a%10;
// if(a<10){
// r=0;
// }else{
// r = a / 10;
// a = a % 10;
// }
n.append(a); // DANS LA BOUCLE
}//FIN BOUCLE
n.reverse();n1.reverse();n2.reverse();// inverser les 2 chaines
System.out.println(n1+" + "+n2+" = " + n );
int i1 = Integer.parseInt(n1.toString());
int i2 = Integer.parseInt(n2.toString());
System.out.println(i1+" + "+i2+" = " + (i1+i2) );
}
} |