In: Computer Science
2. Write a program that asks for hexadecimal number and converts it to decimal. Then change it to convert an octal number to decimal in perl language.
Program to convert an hexadecimal to decimal is as following:
print "Enter a number in Hexadecimal format(i.e., in 0x.. format): ";
$num = <STDIN>;
chomp $num; #removes new line character from input
exit unless defined $num;
$num = hex($num) if $num =~ /^0/;
printf "Number in decimal: %d \n", $num;
Sample output to above program is as following:
Code to convert Octal to decimal is as following:
print "Enter a number in Octal format( i.e, 0...format): ";
$num = <STDIN>;
chomp $num; #removes new line character from input
exit unless defined $num;
$num = oct($num) if $num =~ /^0/;
printf "Number in decimal: %d \n", $num;
Sample output to above program is as following:
Note - if any doubt/query, do ask in comment section