In: Computer Science
If you have any problem with the code feel free to comment.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Factorial</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<script type="text/javascript" src="test.js"></script>
</body>
</html>
CSS
body {
width: 100%;
}
/* centering the table */
table {
margin: 0 auto;
text-align: center;
border-collapse: collapse;
}
/* providing border to the table */
td {
border: 2px solid black;
padding: 10px;
}
/* changing the background color of table header */
.table-header {
background-color: beige;
font-size: 20px;
}
JAVASCRIPT
var n = parseInt(prompt('Enter the number'));//taking user input
//showing table structure
document.write('<table>');
document.write('<tr class="table-header">');
document.write('<td>Value</td>');
document.write('<td>Factorial</td> </tr>');
for(i=1; i<=n; i++){//traversing the user input
var f = findFactorial(i);
document.write('<tr><td>'+i+'</td>');
document.write('<td>'+f+'</td> </tr>');
}
document.write('</table>');
function findFactorial(value){//finding the factorial of the number
var fact=1, j;
for(j=1; j<=value; j++){
fact = fact * j;
}
return fact;
}
OUTPUT