Friday, September 1, 2017

Name In Binary

Code:

package com.nearur;

import java.util.Scanner;

public class NTB {


static StringBuffer buffer=new StringBuffer();
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.print("Enter Your Name:");
String a=in.nextLine();
String[] name=a.split(" ");

for(String x:name) {
convert(x);
}

System.out.println("Binary Code is : "+buffer.toString());
}

static void convert(String name) {

char[] a=name.toCharArray();
for(char x: a) {
binary((int)x);
buffer.append(" ");
}

}
static void binary(int n) {
while(n>0) {
if(n%2==0) {
buffer.append("0");
}else {
buffer.append("1");
}
n=n/2;
}
}


}


Output: