Está en la página 1de 3

SUMA

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package convertir_numeros;
import java.util.Scanner;
/**
*
*/
public class suma {
public static void main(String[] args) {
Scanner leer=new Scanner(System.in);
String n1="";
String n2="";
System.out.println("Ingrese su numero en binario");//se pide el numero en binario
n1=leer.next();//se guarda nuestro numero 1
System.out.println("Ingrese su numero binario numero 2");//aqui se pide el segundo
numero
n2=leer.next();//guardara nuestro segundo numero

System.out.println("Su resultado es: "+Suma(n1,n2));


}
public static String Suma(String n1, String n2){
int a1=Integer.parseInt(n1,2);//recibe nnumeros binarios
int a2=Integer.parseInt(n2,2);//recibe el segundo numero en binario
int c=a1+a2;//esta linea hace la operacion
String resultado=Integer.toString(c ,2);//esta linea covierte en string y muestra
los numeros binarios
return resultado;
}
}

RESTA
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package convertir_numeros;
import java.util.Scanner;
/**
*
*/
public class resta {
public static void main(String[] args) {
Scanner leer=new Scanner(System.in);
String n1="";
String n2="";
System.out.println("Ingrese su numero en binario");//se pide el numero en binario
n1=leer.next();//se guarda nuestro numero 1
System.out.println("Ingrese su numero binario numero 2");//aqui se pide el segundo
numero
n2=leer.next();//guardara nuestro segundo numero

System.out.println("Su resultado es: "+resta(n1,n2));


}
public static String resta(String n1, String n2){
int a1=Integer.parseInt(n1,2);//recibe nnumeros binarios
int a2=Integer.parseInt(n2,2);//recibe el segundo numero en binario
int c=a1-a2;//esta linea hace la operacion
String resultado=Integer.toString(c ,2);//esta linea covierte en string y muestra
los numeros binarios

return resultado;
}
}

También podría gustarte