0% found this document useful (0 votes)
19 views

Suma, Resta, Multiplicacion Y Division en Numeros Binarios

The document discusses binary number arithmetic and conversion between number bases. It contains code for adding numbers in octal, and recursively converting decimal numbers to binary, octal and hexadecimal. Functions are defined to perform addition of octal numbers, and to convert a decimal number to binary, octal and hexadecimal through recursive calls.

Uploaded by

Lector Pross
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Suma, Resta, Multiplicacion Y Division en Numeros Binarios

The document discusses binary number arithmetic and conversion between number bases. It contains code for adding numbers in octal, and recursively converting decimal numbers to binary, octal and hexadecimal. Functions are defined to perform addition of octal numbers, and to convert a decimal number to binary, octal and hexadecimal through recursive calls.

Uploaded by

Lector Pross
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

SUMA, RESTA, MULTIPLICACION Y DIVISION EN NUMEROS

BINARIOS
package programa;

import java.util.Scanner;

public class Programa {

static int a, b;

public static void main(String[] args)

Scanner leer = new Scanner(System.in);

System.out.println("Introduzca el primer octal");

String valor = leer.nextLine();

try

int valord = Integer.parseInt(valor, 8 );

catch (NumberFormatException e)

System.out.println("El primer número debe ser octal");

System.out.println("Introduzca el segundo octal");

String valorb = leer.nextLine();

try

{
int valordb = Integer.parseInt(valorb, 8 );

catch (NumberFormatException e)

System.out.println("El segundo número debe ser octal");

String res = "";

int val;

int mayor = valor.length() > valorb.length() ? valor.length() : valorb.length();

int acarreo = 0;

boolean seguir = true;

for (int cont = mayor-1; cont >= 0; cont--)

val = 0;

try

a = Character.getNumericValue(valor.charAt(cont));

catch (StringIndexOutOfBoundsException e)

a = 0;

seguir = false;

try

b = Character.getNumericValue(valorb.charAt(cont));
}

catch (StringIndexOutOfBoundsException e)

b = 0;

seguir = false;

val = a + b;

if (acarreo > 0)

val += acarreo;

acarreo = 0;

if (val >= 10)

acarreo = val/10;

if (val > 7)

val -= 8;

res += val;

if (!seguir)

if (val > 0) res += val;

else if (acarreo > 0) res += val;


}

res = new StringBuffer(res).reverse().toString();

System.out.println(res);

SUMA DE NUMEROS EN SISTEMA OCTAL


package programa;

import java.util.Scanner;

public class Programa {

static int a, b;

public static void main(String[] args)

Scanner leer = new Scanner(System.in);

System.out.println("Introduzca el primer octal");

String valor = leer.nextLine();


try

int valord = Integer.parseInt(valor, 8 );

catch (NumberFormatException e)

System.out.println("El primer número debe ser octal");

System.out.println("Introduzca el segundo octal");

String valorb = leer.nextLine();

try

int valordb = Integer.parseInt(valorb, 8 );

catch (NumberFormatException e)

System.out.println("El segundo número debe ser octal");

String res = "";

int val;

int mayor = valor.length() > valorb.length() ? valor.length() : valorb.length();

int acarreo = 0;

boolean seguir = true;

for (int cont = mayor-1; cont >= 0; cont--)

val = 0;

try

a = Character.getNumericValue(valor.charAt(cont));
}

catch (StringIndexOutOfBoundsException e)

a = 0;

seguir = false;

try

b = Character.getNumericValue(valorb.charAt(cont));

catch (StringIndexOutOfBoundsException e)

b = 0;

seguir = false;

val = a + b;

if (acarreo > 0)

val += acarreo;

acarreo = 0;

if (val >= 10)


acarreo = val/10;

if (val > 7)

val -= 8;

res += val;

if (!seguir)

if (val > 0) res += val;

else if (acarreo > 0) res += val;

res = new StringBuffer(res).reverse().toString();

System.out.println(res);

}
Convertir decimal a binario, octal y hexadecima
import java.util.Scanner;
public class Ejemplo1
{
public static void main(String[]args){
Scanner leer = new Scanner(System.in);
System.out.print("Ingrese una cifra: ");
int cifra = leer.nextInt();
System.out.print("\nbinario:\t");
binario(cifra);
System.out.print("\noctal:\t\t");
octal(cifra);
System.out.print("\nHexadecimal:\t");
hexadecimal(cifra);

//SE IMPLEMENTA UN METODO RECURCIVO PARA TRANSFORMARA A BINARIO LA


CIFRA
public static void binario(int N){
if(N == 1) System.out.print("1");
else{
binario(N/2);
System.out.print(N%2);
}
}

//SE IMPLEMENTA UN METODO RECURCIVO PARA TRANSFORMARA A OCTAL LA CIFRA


public static void octal(int N){
if(N < 8) System.out.print(N);
else {
octal(N/8);
System.out.print(N%8);
}
}

//SE IMPLEMENTA UN METODO RECURCIVO PARA TRANSFORMARA A HEXADECIMAL LA


CIFRA
public static void hexadecimal(int N){
if(N < 16){
if(N == 10) System.out.print("A");
if(N == 11) System.out.print("B");
if(N == 12) System.out.print("C");
if(N == 13) System.out.print("D");
if(N == 14) System.out.print("E");
if(N == 15) System.out.print("F");
if(N < 10) System.out.print(N);
}
else{
hexadecimal(N/16);
System.out.print(N%16);
}
}
}

You might also like