viwanshu vaibhaw
question
1 Answer(s)      2 years and 7 months ago
Posted in : JavaScript Questions

write a menu driven program to accept a number belonging to any number system that is octal,binary,decimal,hexadecimal according to user's choice and display its equivalent number system entered according to the user's choice

View Answers

December 2, 2010 at 4:27 PM


Hi Friend,

Try the following code:

<html>
<script>
function getNumber(){
var num=document.getElementById('num').value;
var type=document.getElementById('type').value;
var sel=window.prompt("Select Number System:\n1 for Octal\n2 for Binary\n3 for Decimal\n4 for Hexadeciaml");
sel=parseInt(sel);
switch(sel){
case 1:
if(type=="decimal"){
num=parseInt(num);
var octal = num.toString(8);
alert(octal);
}
else if(type=="octal"){
alert("Entered value is in octal!");
}
else if(type=="hexadecimal"){
var number=parseInt(num,16);
alert(number.toString(8));

}
else if(type=="binary"){
var number=parseInt(num,2);
alert(number.toString(8));
}
break;

case 2:
if(type=="decimal"){
num=parseInt(num);
var binary = num.toString(2);
alert(binary);
}
else if(type=="octal"){
var number=parseInt(num,8);
alert(number.toString(2));
}
else if(type=="hexadecimal"){
var number=parseInt(num,16);
alert(number.toString(2));
}
else if(type=="binary"){
alert("Entered values is in binary!");
}
break;

case 3:
if(type=="decimal"){
alert("Entered value is in decimal!");
}
else if(type=="octal"){
var number=parseInt(num,8);
alert(number);
}
else if(type=="hexadecimal"){
var number=parseInt(num,16);
alert(number);
}
else if(type=="binary"){
var number=parseInt(num,2);
alert(number);
}
break;

case 4:
if(type=="decimal"){
num=parseInt(num);
var hexadecimal = num.toString(16);
alert(hexadecimal);
}
else if(type=="octal"){
var number=parseInt(num,8);
alert(number.toString(16));
}
else if(type=="hexadecimal"){
alert("Entered value is in hexadecimal!");
}
else if(type=="binary"){
var number=parseInt(num,2);
alert(number.toString(16));
}
break;
default:
}
}
</script>
Enter Number <input type="text" id="num"><br><br>
Enter Type      <input type="text" id="type"><sub>binary,octal,decimal,hexadecimal</sub><br><br>
<input type="button" onclick="getNumber();" value="Get">
</html>

Thanks









Related Pages:
question
question   sir plz tell me what should i give in title box. just i want java program for the question typed in this area
question
question   sir plz tell me what should i give in title box. just i want java program for the question typed in this area
Question
Question   When there is an exception in my program how java runtime system handles
question
question  dear sir/madam my question is how to compare two text format in java..we are java beginners..so we need the complete source code for above mentioned question...we have to compare each and every word
question
question  Dear sir i had some typing mistake at previous question so its my humble request to let me know the steps to start the tomcat6 under the tomcat directory
question
question  Gud morning sir, I have asked u some question regarding jsp in saturaday for that i didnot find any answere in which u send me the some of the links.U have asked me the specify some details. There is a entity name
Question?
Question?  My question is how to: Add a menu bar to the program with a File menu. In the File menu, add a submenu (JMenuItem) called About. When the user clicks on the About menu item, display a JOptionPane message dialog