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

Assignment - 3

Abcgdgdsrtfd

Uploaded by

banunazama083
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Assignment - 3

Abcgdgdsrtfd

Uploaded by

banunazama083
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Assignment- 3

1. Write a Java Program to Convert Binary to Octal Conversion


import java.util.Scanner;
public class BinaryToOctalConverter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

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


String binaryInput = scanner.nextLine();

// Validate if the input is a valid binary number


if (!isValidBinary(binaryInput)) {
System.out.println("Invalid binary number. Please enter a valid binary number.");
scanner.close();
return;
}

// Convert binary to octal


String octalOutput = binaryToOctal(binaryInput);
System.out.println("Octal equivalent: " + octalOutput);

scanner.close();
}

// Function to check if a string is a valid binary number


public static boolean isValidBinary(String binary) {
for (int i = 0; i < binary.length(); i++) {
char digit = binary.charAt(i);
if (digit != '0' && digit != '1') {
return false;
}
}
return true;
}

// Function to convert a binary number to octal


public static String binaryToOctal(String binary) {
int len = binary.length();

// Make the binary string length a multiple of 3 by adding leading zeros


int padding = len % 3;
if (padding > 0) {
binary = "0".repeat(3 - padding) + binary;
len += (3 - padding);
}
StringBuilder octal = new StringBuilder();
for (int i = 0; i < len; i += 3) {
String triplet = binary.substring(i, i + 3);
int decimalValue = Integer.parseInt(triplet, 2);
octal.append(Integer.toOctalString(decimalValue));
}

return octal.toString();
}
}
Output:-

Variable declaration table

Variable Name Variable Type/Data type Description

2. Write a Java Program to Convert Octal to Decimal Conversion


public class Octal_Decimal {

public static void main(String args[])


{
// octal value
String onum = "157";

// octal to decimal using Integer.parseInt()


int num = Integer.parseInt(onum, 8);

System.out.println(
"Decimal equivalent of Octal value 157 is: "
+ num);
}
}
Output:-

Variable declaration table

Variable Name Variable Type/Data type Description

3. Write a Java Program For Decimal to Octal Conversion.


import java.util.Scanner;

public class DecimalToOctalConverter {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter a decimal number: ");


int decimalInput = scanner.nextInt();

// Convert decimal to octal


String octalOutput = decimalToOctal(decimalInput);
System.out.println("Octal equivalent: " + octalOutput);

scanner.close();
}

// Function to convert a decimal number to octal


public static String decimalToOctal(int decimal) {
StringBuilder octal = new StringBuilder();
if (decimal == 0) {
octal.append(0);
} else {
while (decimal > 0) {
int octalDigit = decimal % 8;
octal.insert(0, octalDigit);
decimal /= 8;
}
}
return octal.toString();
}
}
Output:-

Variable declaration table

Variable Name Variable Type/Data type Description

4. Write a Java Program For Hexadecimal to Decimal conversion.


public class HexToDec {
// main function
public static void main(String[] args)
{
String hex1 = "1AB";
String hex2 = "1A";
int dec1 = hexToDec(hex1);
int dec2 = hexToDec(hex2);
System.out.println(hex1 + " in decimal is " + dec1);
System.out.println(hex2 + " in decimal is " + dec2);
}

private static int hexToDec(String hex)


{
int len = hex.length();
int dec = 0;
for (int i = 0; i < len; i++) {
char c = hex.charAt(i);
int digit = Character.digit(c, 16);
dec = dec * 16 + digit;
}
return dec;
}
}
Output:-

Variable declaration table

Variable Name Variable Type/Data type Description

5. Write a Java Program For Decimal to Hexadecimal Conversion.


// To convert Decimal to
// Hexadecimal Number
import java.util.*;

// Main driver method


public class Main {
public static void main(String[] args)
{
// Number in decimal
int number = 2545;

// output
System.out.println(Integer.toString(number, 16));
}
}
Output:-

Variable declaration table

Variable Name Variable Type/Data type Description

6. Write a Java Program for Decimal to Binary Conversion.


// Java program to Decimal to binary conversion
// using bitwise operator
// Size of an integer is assumed to be 32 bits

class gfg {
// Function that convert Decimal to binary
public void decToBinary(int n)
{
// Size of an integer is assumed to be 32 bits
for (int i = 31; i >= 0; i--) {
int k = n >> i;
if ((k & 1) > 0)
System.out.print("1");
else
System.out.print("0");
}
}
}

class geek {
// driver code
public static void main(String[] args)
{
gfg g = new gfg();
int n = 32;
System.out.println("Decimal - " + n);
System.out.print("Binary - ");
g.decToBinary(n);
}
}
Output:-

Variable declaration table

Variable Name Variable Type/Data type Description

7. Write a Java Program for Decimal to Binary Conversion by using bitwise operator.
// Java program to Decimal to binary conversion
// using bitwise operator
// Size of an integer is assumed to be 32 bits

class Demimaltobinary{
// Function that convert Decimal to binary
public void decToBinary(int n)
{
// Size of an integer is assumed to be 32 bits
for (int i = 31; i >= 0; i--) {
int k = n >> i;
if ((k & 1) > 0)
System.out.print("1");
else
System.out.print("0");
}
}
}
class geek {
// driver code
public static void main(String[] args)
{
gfg g = new gfg();
int n = 32;
System.out.println("Decimal - " + n);
System.out.print("Binary - ");
g.decToBinary(n);
}
}
Output:-

Variable declaration table

Variable Name Variable Type/Data type Description

8. Boolean toString() method in Java.


// java code to demonstrate
// Boolean toString() method

class tostring_ {
public static void main(String[] args)
{

// boolean type value


boolean value = true;

// static toString() method of Boolean class


String output = Boolean.toString(value);

// printing the value


System.out.println(output);
}
}
Output:-

Variable declaration table

Variable Name Variable Type/Data type Description

9. public String toString()


// java code to demonstrate
// Boolean toString() method

class _tostring {
public static void main(String[] args)
{

// creating a Boolean object


Boolean b = new Boolean(true);

// toString method of Boolean class


String output = b.toString();

// printing the output


System.out.println(output);
}
}
Output:-

Variable declaration table

Variable Name Variable Type/Data type Description

10. Example -2
// Java code to demonstrate
// Boolean toString() method

class _tostring{
public static void main(String[] args)
{

// creating a Boolean object


Boolean b = new Boolean(false);

// toString method of Boolean class


String output = b.toString();

// printing the output


System.out.println(output);
}
}
Output:-

Variable declaration table

Variable Name Variable Type/Data type Description

11. Convert String to Double in Java by using parseDouble() Method of Double Class.
// Java program to convert String to Double
// Using parseDouble() Method of Double Class
// Main class
public class string todouble {

// Main driver method


public static void main(String args[])
{

// Create and initializing a string


String str = "2033.12244";

// Converting the above string into Double


// using parseDouble() Method
double str1 = Double.parseDouble(str);

// Printing string as Double type


System.out.println(str1);
}
}
Output:-

Variable declaration table

Variable Name Variable Type/Data type Description

12. Convert String to Double in Java by Using valueOf() Method of Double Class.
// Java program to convert String to Double
// using valueOf() Method of Double Class

// Main class
public class valueof_ {
// Main driver method
public static void main(String args[])
{
// Creating and initializing a string
String str = "2033.12244";

// Converting the above string to Double type


double str1 = Double.valueOf(str);

// Printing above string as double type


System.out.println(str1);
}
}
Output:-

Variable declaration table

Variable Name Variable Type/Data type Description

13. Convert String to Double in Java by Using the Constructor of Double Class.
// Java program to convert String to Double
// Using Constructor of Double class

// Main class
public class __doubleclass{

// Main driver method


public static void main(String args[])
{

// Creating and initializing a string


String str = "2033.12244";

// Converting above string into double type


Double str1 = new Double(str);

// print above string as Double type


System.out.println(str1);
}
}
Output:-

Variable declaration table

Variable Name Variable Type/Data type Description

14. Java Program to Convert Double to String by Using valueOf() Method of String Class.
// Java Program to Convert Double Data to a String Data
// Using valueOf() Method of String Class

// Importing Libraries
import java.io.*;
import java.util.*;

// Driver Class
class __valueofdouble {
// Main driver function
public static void main(String[] args)
{
// Declaring and initializing double number
double number = 123.456;

// Converting Double data to String data


String output = String.valueOf(number);

// Printing the above string


System.out.println(output);
}
}
Output:-

Variable declaration table

Variable Name Variable Type/Data type Description

15. Java Program to Convert Double to String Using toString() Method.


// Java Program to Convert Double Data to a String Data
// Using toString() method of Double Class

// Importing Libraries
import java.io.*;
import java.util.*;

// Driver Class
class methods__doubleclass {
// Main driver method
public static void main(String[] args)
{
// Declaring and initializing number
double number = 123.456;

// Converting Double data to String data


String output = Double.toString(number);

// Printing above string on console


System.out.println(output);
}
}
Output:-

Variable declaration table

Variable Name Variable Type/Data type Description

You might also like