1. Write
a program to convert a string into number
2. Write
a program to convert decimal number to binary number
3. Write
a program to convert binary number to decimal number
4. Write
a program to check given number is binary or not
Solution of all above problem in below program.
public final class Convertor {
private Convertor() {
}
public static void StringToint(String
numString) {
int num = 0;
char str[] = numString.toCharArray();
int ascii_0 = (int) '0';
for (char c : str) {
num = (num * 10) + ((int) c -
ascii_0);
}
System.out.println(num);
}
public static void decimalToBinary(int
decimalNum) {
int index = 0;
int dec[] = new int[20];
do {
dec[index++] = decimalNum % 2;
decimalNum = decimalNum / 2;
} while (decimalNum > 0);
for(int x=index-1;x>=0;x--){
System.out.print(dec[x]);
}
}
public static void binaryToDeciamal(int
binaryNum) {
int n = 0;
int num = 0;
do {
int k = binaryNum % 10;
num += k * Math.pow(2, n);
binaryNum = binaryNum / 10;
n++;
} while (binaryNum > 0);
System.out.println(num);
}
public static void isBinary(int binaryNum)
{
boolean flag = false;
do {
int num = binaryNum % 10;
if (num > 1) {
flag = true;
}
binaryNum = binaryNum / 10;
} while (binaryNum > 0);
if (flag == true) {
System.out.println("Not a
binary number");
} else {
System.out.println("Yes, It is
a binary number");
}
}
}
No comments:
Post a Comment