Tuesday, February 3, 2026

Java program to convert Decimal to Binary

 import java.util.Scanner;

class D2B

{

public static void main(String[] args)

{

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

Scanner sc = new Scanner(System.in);

int num = sc.nextInt();

String binary = "";

while(num/2 > 0)

{

binary = num%2 + binary;

num = num/2;

}

binary = 1 + binary;

System.out.print(binary);

}

}


Output:



java string value + int is a string or not?

When a Java String value is concatenated with an int value using the + operator, the result is a String.
This behavior is due to Java's rules for the + operator:
  1. If either operand is a String, the other operand is automatically converted to its string representation (e.g., the integer 10 becomes "10").
  2. The operator then performs string concatenation.

Example:

 String value = "The answer is: ";

int number = 42;


String result = value + number;


// The value of 'result' will be the string "The answer is: 42"


Java program to convert Decimal to Binary

 import java.util.Scanner; class D2B { public static void main(String[] args) { System.out.print("Enter a decimal number :"...