NOOR2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

NAME:NOOR UL

HUDA
]

REGNO:SP23-BCS-
127
Lab Task 1:
Covert the following programs Pseudo-code in JAVA.Just use the algorithms given in
activity.
1. Conversion from base 2 and base 16 numbers to Decimal

package javaapplication1;
import java.util.*;

public class JavaApplication1 {

public static void main(String[] args) {


task1 ob = new task1();
ob.input();
ob.convert();
}

class task1{

// binary to decimal
String b;
String h;
char[] c=new char[32];
int decimal=0;
int hexa=0;
Scanner s=new Scanner(System.in);

void input(){
System.out.println("Enter a binary number");
b=s.next();
System.out.println("Enter a hexadecimal number");
h=s.next();

void convert(){
for(int i=0 ; i< b.length() ; i++){
c[i]=b.charAt(b.length()-1-i);
if(c[i]=='1'){
decimal += Math.pow(2, i);
}
}
for(int i=0 ; i< h.length() ; i++){
c[i]=h.charAt(h.length()-1-i);
int value;

if (c[i] >= '0' && c[i] <= '9') {


value = c[i] - '0';
} else if (c[i]>= 'A' && c[i] <= 'F') {
value = c [i]- 'A' + 10;
} else if (c[i] >= 'a' && c[i] <= 'f') {
value = c[i]- 'a' + 10;
} else {
System.out.println("Invalid hexadecimal input.");
return;
}
hexa += value * Math.pow(16, i);
}
System.out.println("binary to decimal "+ decimal);
System.out.println("hexa to decimal="+ hexa );

}
}

Lab Task 2
Conversion from decimal number to base 2 and base 16
package com.mycompany.binary_dec;

import java.util.Scanner;

public class Binary_decimal {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int decimal;

System.out.println(" Enter a number: ");

decimal = scanner.nextInt();

System.out.println(" Decimal to binary ");

String binary="";

while(decimal!=0){

int reminder=decimal%2;

binary+= reminder;

decimal/=2;

System.out.println(binary);
System.out.println(" Decimal to hexadecimal ");

String hexa="";

char[] hexaDigits = new char[] {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

while(decimal!=0){

int rem=decimal%16;

hexa = hexaDigits[rem] + hexa;

decimal/=16;

System.out.println(binary);

Lab Task 3
Conversion of ay Binary fraction to Decimal

package com.mycompany.binaryfractiontodecimal;

import java.util.Scanner;

public class BinaryFractionToDecimal {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter a binary fraction: ");

String binary = scanner.nextLine();

String[] parts = binary.split("\\.");

int integerPart = convertIntegerPart(parts[0]);

double fractionalPart = 0;

if (parts.length > 1) {
fractionalPart = convertFractionalPart(parts[1]);

double decimalResult = integerPart + fractionalPart;

System.out.println("Decimal equivalent: " + decimalResult);

public static int convertIntegerPart(String integerPart) {

int decimalValue = 0;

int length = integerPart.length();

for (int i = 0; i < length; i++) {

if (integerPart.charAt(i) == '1') {

decimalValue += Math.pow(2, length - 1 - i);

return decimalValue;

public static double convertFractionalPart(String fractionalPart) {

double decimalValue = 0.0;

int length = fractionalPart.length();

for (int i = 0; i < length; i++) {

if (fractionalPart.charAt(i) == '1') {

decimalValue += 1.0 / Math.pow(2, i + 1);

return decimalValue;
}

You might also like