Wednesday, 19 April 2017

Javascript Program for Adders Such as Full Adder,Half Adder,Full Substractor and Half Substractor

<html>
<head><title>Gates</title>
<script>
function fulla()
{ var c,s,d;
document.write("<table border=3>");
document.write("<tr><th colspan=3>Input</th><th colspan=2>Output</th></tr>");
document.write("<tr><th>A</th><th>B</th><th>C</th><th>Carry</th><th>Sum</th></tr>");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
for(k=0;k<=1;k++)
{
d=i+j+k;
if(d==0)
{
s=0;
c=0;
document.write("<tr><td>" +i+ "</td><td>" +j+ "</td><td>" +k+ "</td><td>"+c+ "</td><td>"+s+ "</td></tr>");
}
else if(d==1)
{
s=1;
c=0;
document.write("<tr><td>" +i+ "</td><td>" +j+ "</td><td>" +k+ "</td><td>"+c+ "</td><td>"+s+ "</td></tr>");
}
else if(d==2)
{
s=0;
c=1;
document.write("<tr><td>" +i+ "</td><td>" +j+ "</td><td>" +k+ "</td><td>"+c+ "</td><td>"+s+ "</td></tr>");
}
else
{
s=1;
c=1;
document.write("<tr><td>" +i+ "</td><td>" +j+ "</td><td>" +k+ "</td><td>"+c+ "</td><td>"+s+ "</td></tr>");
}
}
}
}
}
function halfa()
{ var c,s,d;
document.write("<table border=3>");
document.write("<tr><th colspan=2>Input</th><th colspan=2>Output</th></tr>");
document.write("<tr><th>A</th><th>B</th><th>Carry</th><th>Sum</th></tr>");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
d=i+j;
if(d==0)
{
s=0;
c=0;
document.write("<tr><td>" +i+ "</td><td>" +j+ "</td><td>"+c+ "</td><td>"+s+ "</td></tr>");
}
else if(d==1)
{
s=1;
c=0;
document.write("<tr><td>" +i+ "</td><td>" +j+ "</td><td>"+c+ "</td><td>"+s+ "</td></tr>");
}
else
{
s=0;
c=1;
document.write("<tr><td>" +i+ "</td><td>" +j+ "</td><td>"+c+ "</td><td>"+s+ "</td></tr>");
}
}
}
}
function fulls()
{ var b,s,d;
document.write("<table border=3>");
document.write("<tr><th colspan=3>Input</th><th colspan=2>Output</th></tr>");
document.write("<tr><th>A</th><th>B</th><th>C</th><th>Difference</th><th>Borrow</th></tr>");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
for(k=0;k<=1;k++)
{
d=i+j+k;
if(d==0)
{
s=0;

b=(s-i-j-k)/2;
if(b==-1)
{
b=1;
}
document.write("<tr><td>" +i+ "</td><td>" +j+ "</td><td>" +k+ "</td><td>"+s+ "</td><td>"+b+ "</td></tr>");
}
else if(d==1)
{
s=1;

b=(s-i-j-k)/2;
if(b==-1)
{
b=1;
}
document.write("<tr><td>" +i+ "</td><td>" +j+ "</td><td>" +k+ "</td><td>"+s+ "</td><td>"+b+ "</td></tr>");
}
else if(d==2)
{
s=0;

b=(s-i-j-k)/2;
if(b==-1)
{
b=1;
}
document.write("<tr><td>" +i+ "</td><td>" +j+ "</td><td>" +k+ "</td><td>"+s+ "</td><td>"+b+ "</td></tr>");
}
else
{
s=1;

b=(s-i-j-k)/2;
if(b==-1)
{
b=1;
}
document.write("<tr><td>" +i+ "</td><td>" +j+ "</td><td>" +k+ "</td><td>"+s+ "</td><td>"+b+ "</td></tr>");
}

}
}
}
}
function halfs()
{ var b,s,d;
document.write("<table border=3>");
document.write("<tr><th colspan=2>Input</th><th colspan=2>Output</th></tr>");
document.write("<tr><th>A</th><th>B</th><th>Difference</th><th>Borrow</th></tr>");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
s=i+j;
b=i*j;
if(s==0)
{
s=1;
}
else
{
s=0;
}
if(b==0)
{
b=1;
}
else
{
b=0;
}
document.write("<tr><td>" +i+ "</td><td>" +j+ "</td><td>"+s+ "</td><td>"+b+ "</td></tr>");

}
}
}

</script>
</head>
<body>
<form name="f1">
<input type="Button" name="habtn" value=" Half adder" onClick="halfa()">
<input type="Button" name="fabtn" value="Full adder" onClick="fulla()">
<input type="Button" name="hsbtn" value="Half Substracter" onClick="halfs()">
<input type="Button" name="fsbtn" value="FullSubstracter" onClick="fulls()">
</form>
</body>

</html>

Output:-




No comments:

Post a Comment

Popular Posts

Program of Counting Vowels and Consonants in Javascript

Solution:-  <html> <head><title>Count vowels and consonants</title> <script language="Javascript"...