Skip to content

Instantly share code, notes, and snippets.

@sanrodari
Forked from godie007/sumaDecimal.java
Created June 14, 2012 16:38
Show Gist options
  • Save sanrodari/2931384 to your computer and use it in GitHub Desktop.
Save sanrodari/2931384 to your computer and use it in GitHub Desktop.
package Trabajo;
public class SumarDecimal {
public static void main(String[] args) {
sumaDecimal();
}
private static void sumaDecimal() {
String primerOperando = "5";
String segundoOperando = "9";
int[] arreglo1 = convertirCadenaArreglo(primerOperando);
int[] arreglo2 = convertirCadenaArreglo(segundoOperando);
int loQuellevo = 0;
int[] resultado = new int[arreglo1.length];
for (int i = 0; i < arreglo1.length; i++) {
int suma = arreglo1[i] + arreglo2[i]+loQuellevo;
if (suma < 10) {
loQuellevo = 0;
resultado[i] = suma;
}
if (suma >= 10) {
loQuellevo = 1;
resultado[i] = suma - 10;
}
else {
loQuellevo = 0;
resultado[i] = suma;
}
}
for (int j = 0; j < resultado.length; j++) {
System.out.println(resultado[j]);
}
}
/**
*
* @param operando
* invierte y da una misma longitud a los 2 arreglos
* @return
*/
public static int[] convertirCadenaArreglo(String operando) {
int[] arrreglo = new int[10];
for (int i = 0; i < arrreglo.length; i++) {
arrreglo[i] = 0;
}
for (int i = operando.length() - 1; i >= 0; i--) {
arrreglo[i] += Integer.parseInt(""
+ operando.charAt(operando.length() - 1 - i));
}
return arrreglo;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment