Program 5 : BASE N TO
DECIMAL
_____________________________________________
import java.util.Scanner;
public class BaseNToDecimal {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number in base n: ");
String number = sc.nextLine();
System.out.print("Enter the base: ");
int base = sc.nextInt();
int decimal = Integer.parseInt(number, base);
System.out.println("Decimal value: " + decimal);
}
}
ALGORITHM
_____________________________________________
Step 1: Start the program and prompt the user to enter a decimal
number.
Step 2: Store the user input in the variable decimal.
Step 3: Prompt the user to enter the base (e.g., 2 for binary, 8 for
octal, 16 for hexadecimal, etc.).
Step 4: Store the user input in the variable base.
Step 5: Convert the decimal number to the specified base using
the method Integer.toString(decimal, base).
Step 6: Store the converted value in the variable result.
Step 7: Display the value of result to the user, indicating the
number in the specified base.
OUTPUT
_____________________________________________
Variable Table
_____________________________________________